Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiline TextBox Scrollbar Delimma 1

Status
Not open for further replies.

PatrioticTX

Programmer
Dec 5, 2001
18
US
I would like to hide the scrollbar on a multiline textbox if the amount of text does not exceed the visible bounds. For example, if the textbox height allows the display of two text lines, and up to two lines are typed in, I do not want the scrollbar to be visible (VB default is to disable the scrollbar). Once the number of lines typed exceeds two, I want the scrollbar to be visible. Note: I want this to occur with wrappable text... no CRLFs.

I was originally going to set the scrollbar to be visible when the control had focus, but the
Code:
scrollbars
property is readonly at runtime.

Any suggestions would be appreciated!
 
Yeah, multiline textboxes don't have much functionality concerning their scrollbars. Here's what you might do, though. At design-time set the scrollbars property to Vertical so you just have the vertical scrollbar. Then make a blank label control and size it to overlap the scrollbar. Then have a test to see if there are more than two lines. If so, set the label control's visible property to False.

The hard part is that there is no easy way in visual basic to find out how many lines are in the multiline textbox. You can do a loop and search for CrLf, but no way to look for wrapped lines.
 
Here's a function that allows you to show and hide a vertical scroll bar in a textbox at run time:
[tt]
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_VSCROLL = &H200000

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4

Private Function SetVScroll(TargetTextBox As textBox, Visible As Boolean) As Boolean
Dim CurrentStyle As Long
CurrentStyle = GetWindowLong(TargetTextBox.hwnd, GWL_STYLE)
If Visible Then
' Make visible by changing window style
SetWindowLong TargetTextBox.hwnd, GWL_STYLE, CurrentStyle Or WS_VSCROLL
ElseIf CurrentStyle And WS_VSCROLL Then
' Make invsible if visible by changing window style
SetWindowLong TargetTextBox.hwnd, GWL_STYLE, CurrentStyle Xor WS_VSCROLL
End If
' Scrollbar style is cached, so we need to do SetWindowPos to activate the style change
' Without this line the status of the scrollbar will NOT change
SetWindowPos TargetTextBox.hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED + SWP_NOMOVE + SWP_NOSIZE + SWP_NOZORDER
SetVScroll = True
End Function
 
Oh - and you can get the number of lines in a (multiline)textbox with:
[tt]
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_HSCROLL = &H114
Private Const EM_GETLINECOUNT = &HBA


LineCount = = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, 0&)
 
Sypher2 mentioned that no easy way exists to find out how many lines are in a wrapped multiline textbox, so I'll stick with strongm's method for changing scrollbar visibility on the focus events. Thank you both for your help.
 
Strongm, we must have replied at the same time... I didn't get your post until I sent in my first reply. It worked great. Thank you for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top