HOWTO: Extend the Scrolling Capabilities of a TextBox Control
ID: Q161270
|
The information in this article applies to:
-
Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
SUMMARY
Using the Windows API function SendMessage, you can scroll text a specified
number of lines vertically and/or a specified number of columns
horizontally in a text box. You can also scroll text programmatically,
without any user interaction. This technique extends a text box's scrolling
capabilities beyond those provided by its built-in properties and methods.
MORE INFORMATION
In Visual Basic, you can scroll text in a text box vertically or
horizontally by actively clicking the vertical or horizontal scroll bar
respectively, at run time. However, the text always scrolls one line or one
column per click of the scroll bar. Furthermore, there are no built-in
properties or methods to scroll text without user interaction. To work
around these limitations, you can call the Windows API SendMessage function
using the EM_LINESCROLL message. SendMessage requires the following
parameters:
SendMessage(hWnd, EM_LINESCROLL, wParam, lParam) where:
- hWnd is the hWnd of the text box.
- wParam is used to specify the number of horizontal columns to scroll. A
positive value causes text to scroll to the left. A negative value
causes text to scroll to the right.
- lParam is used to specify the number of vertical lines to scroll. A
positive value causes text to scroll upward. A negative value causes
text to scroll downward.
SendMessage returns True if the text box is multiline and False if it is
single-line. Note that calling SendMessage to scroll text vertically does
not require a vertical scroll bar, but the length of text within the text
box should exceed the text box height. To scroll text horizontally, a
scroll bar is required.
Step-by-Step Example
- Start a new Standard EXE project. Form1 is added by default.
- Add a TextBox control, Text1, to Form1 and change its MultiLine property
to True and its ScrollBars property to 3-Both.
- Add two CommandButton controls, Command1 and Command2, to Form1.
- Add the following code to the General Declarations section of Form1:
Const EM_LINESCROLL = &HB6
Private Declare Function SendMessage Lib "User32" Alias _
"SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Long
Private Sub Form_Load()
Dim intLineIndex As Integer, intWordIndex As Integer
'Initialize Text1.
Text1.Font = "Courier New"
Text1.Text = ""
For intLineIndex = 1 To 25 'Add 25 lines of text.
Text1.Text = Text1.Text & "Line" & Str$(intLineIndex)
For intWordIndex = 1 To 5 'Make each line 12 words
'long.
Text1.Text = Text1.Text & " Word" & Str$(intWordIndex)
Next intWordIndex
Text1.Text = Text1.Text & vbCrLf
Next intLineIndex
Command1.Caption = "Vertical"
Command2.Caption = "Horizontal"
End Sub
Private Sub Command1_Click()
Dim lngRet As Long
lngRet = SendMessage(Text1.hWnd, EM_LINESCROLL, 0, 5&)
End Sub
Private Sub Command2_Click()
Dim lngRet As Long
lngRet = SendMessage(Text1.hWnd, EM_LINESCROLL, 5, 0&)
End Sub
- Press the F5 key to run the program. Click "Vertical" to scroll the text
up five lines at a time and click "Horizontal" to scroll the text left
five columns at a time.
Keywords : kbVBp500 kbVBp600 kbGrpVB VBKBCtrl VBKBInt VBKBStd VBKBTextBox VBKBWinAPI
Version :
Platform :
Issue type :
|