HOWTO: Extend the Scrolling Capabilities of a TextBox Control

Last reviewed: July 15, 1997
Article ID: Q161270
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional and Enterprise Editions, for Windows, version 5.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

  1. Start a new Standard EXE project. Form1 is added by default.

  2. Add a TextBox control, Text1, to Form1 and change its MultiLine property to True and its ScrollBars property to 3-Both.

  3. Add two CommandButton controls, Command1 and Command2, to Form1.

  4. 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
    
    

  5. 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          : vb5all vb5howto VBKBCtrl VBKBInt VBKBStd VBKBTextBox VBKBWinAPI kbhowto
Version           : 5.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.