
VB4 Question: Having trouble changing .ScrollBars property
Quote:
> > I'm trying to write a simple text editor. What I want to be able to do is to
> > switch back and forth from wordwrap mode. I have been trying to do this by:
> > text1.ScrollBars = 3 (or 2)
> > Any suggestions? Is there a better way to do this?
> You can't change the text box scrollbars property at runtime. It's read-only.
Here is a form that does what you want. It actually has a text box
inside a panel and when the number of lines is less than the height
of the text box, the panel is shrunk to hide the vertical scroll bars
that are always there. Equally, when there are more lines, the panel
is resized to show the scroll bars. Create a project and cut and paste
this form and run it to see how it works. There is an SDK call to
programatically scroll the text box and one to determine the number of
lines in it.
Hope this helps,
Steve
VERSION 2.00
Begin Form frmTest
BackColor = &H00C0C0C0&
Caption = "Form1"
ClientHeight = 2895
ClientLeft = 1425
ClientTop = 2595
ClientWidth = 4140
Height = 3255
Left = 1365
LinkTopic = "Form1"
ScaleHeight = 2895
ScaleWidth = 4140
Top = 2295
Width = 4260
Begin SSPanel pnlPanel
BackColor = &H00C0C0C0&
BevelInner = 1 'Inset
BevelOuter = 0 'None
BorderWidth = 0
Font3D = 0 'None
Height = 2235
Left = 120
TabIndex = 0
Top = 420
Width = 3855
Begin TextBox txtText
BorderStyle = 0 'None
Height = 1995
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 1
Top = 60
Width = 3555
End
End
Begin Label lblLines
Height = 255
Left = 3000
TabIndex = 2
Top = 60
Width = 855
End
End
Option Explicit
Declare Function SendDlgItemMessage Lib "User" (ByVal hdlg As Integer, ByVal
nIDDlgItem As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam
As Any) As Long
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As
Integer, ByVal wParam As Integer, lParam As Any) As Long
Const WM_USER = &H400
Const EM_GETLINECOUNT = WM_USER + 10
Const EM_LINESCROLL = WM_USER + 6
Sub Form_Load ()
'
' Size the text box to fit the panel
'
txtText.Move 30, 30, pnlPanel.Width - 30, pnlPanel.Height - 30
pnlPanel.Width = txtText.Width + txtText.Left - 200
End Sub
Sub txtText_Change ()
Dim lLines&
'
' Get the number of lines in the text box
'
lLines = SendMessage(txtText.hWnd, EM_GETLINECOUNT, 0, 0)
lblLines.Caption = Format(lLines)
'
' Determine if the scrollbars should be shown
'
If lLines > 10 Then
' Yes
pnlPanel.Width = txtText.Width + (txtText.Left * 2)
Else
' No - hide the scrollbar and scroll box to top
pnlPanel.Width = txtText.Width + txtText.Left - 200
lLines = SendMessage(txtText.hWnd, EM_LINESCROLL, 0, -1)
End If
End Sub