VB and VBA Users Source Code: Controlling the scrolling of a textbox
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Controlling the scrolling of a textbox
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, March 14, 2001
Hits:
832
Category:
Windows API
Article:
The following code shows how to scroll a multiline text box both horizontally and vertically. Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long 'Purpose : Scrolls a Multiline textbox horizontally 'Inputs : lhwndTextbox The handle to the textbox. ' [bScrollRight] If True scrolls to the right, else scrolls left. ' [lNumCharacters] The number of characters to scroll. 'Outputs : Returns non zero number on success. 'Author : Andrew Baker 'Date : 03/09/2000 13:58 'Notes : The .ScrollBars property must include a horizontal scroll bar. 'Revisions : Function TextBoxHorizontalScroll(lhwndTextbox As Long, Optional bScrollRight As Boolean = True, Optional lNumCharacters As Long = 1) As Long Const SB_LINERIGHT = 1, SB_LINELEFT = 0, WM_HSCROLL = &H114 Dim lThisCharacters As Long If bScrollRight Then For lThisCharacters = 1 To lNumCharacters TextBoxHorizontalScroll = SendMessage(lhwndTextbox, WM_HSCROLL, SB_LINERIGHT, ByVal 0&) Next Else For lThisCharacters = 1 To lNumCharacters TextBoxHorizontalScroll = SendMessage(lhwndTextbox, WM_HSCROLL, SB_LINELEFT, ByVal 0&) Next End If End Function 'Purpose : Scrolls a Multiline textbox vertically 'Inputs : lhwndTextbox The handle to the textbox. ' [bScrollUp] If True scrolls upwards, else scrolls downwards. ' [lNumCharacters] The number of characters to scroll. 'Outputs : Returns non zero number on success. 'Author : Andrew Baker 'Date : 03/09/2000 13:58 'Notes : The .ScrollBars property must include a vertical scroll bar. 'Revisions : Function TextBoxVerticalScroll(lhwndTextbox As Long, Optional bScrollUp As Boolean = True, Optional lNumCharacters As Long = 1) As Long Const SB_LINEUP = 0, SB_LINEDOWN = 1, WM_VSCROLL = &H115 Dim lThisCharacters As Long If bScrollUp Then For lThisCharacters = 1 To lNumCharacters TextBoxVerticalScroll = SendMessage(lhwndTextbox, WM_VSCROLL, SB_LINEUP, ByVal 0&) Next Else For lThisCharacters = 1 To lNumCharacters TextBoxVerticalScroll = SendMessage(lhwndTextbox, WM_VSCROLL, SB_LINEDOWN, ByVal 0&) Next End If End Function 'Demonstration code Private Sub Command1_Click() 'Move 5 characters left TextBoxHorizontalScroll Me.Text1.hwnd, False, 5 End Sub Private Sub Command2_Click() 'Move 5 characters right TextBoxHorizontalScroll Me.Text1.hwnd, True, 5 End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder