« Sharpening Your Axis with Visual Basic 9 – Code Magazine Article | Home | More Microsoft “How Do I” Video Updates and Service Pack 1 for 2008 »
Textbox Manipulation in VB and .NET – Using API
By admin | July 21, 2008
This is a basic article on how to do various textbox manipulating that isn't exposed in your normal VB or VB.NET textbox like Page Left or Scroll to the Edge (That I am aware of anyways).
Note: These codes are basically taken from a example I made at my vbcodesource.com site for VB.NET that shows how to do lots and lots of various textbox based manipulating. Just go to http://www.vbcodesource.com/ under the Visual Basic.NET - Examples page.
So in Part #1 of this article I will present some codes that will accomplish what is outlined below to a textbox control...
Page Left
Page Right
Line Left
Line Right
Left Edge
Right Edge
_________________________________
To accomplish these features I will use the tried and true Windows API. Specifically the SendMessage Function is used. Below is the declaration for VB Classic and VB.NET
Visual Basic 6.0
'Used to Send Messages to the control.
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Visual Basic.NET 2002/2003/2005/2008
'
Used to Send Messages to the control.
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
_________________________________
Now for some Constants...
Private Const WM_HSCROLL = &H114
Private Const WM_VSCROLL = &H115
Private Const SB_LINERIGHT = 1
Private Const SB_LINELEFT = 0
Private Const SB_PAGERIGHT = 3
Private Const SB_PAGELEFT = 2
Private Const SB_RIGHT = 7
Private Const SB_LEFT = 6
_________________________________
Now all thats really needed is the textbox you want to send messages to and calling the sendmessage api with the right combination of contants to perform the intended function.
_________________________________
NOTE: If your using VB 5.0 or VB 6.0 then change some small things below...
Change - txtControl.Handle.ToInt32 to txtControl.hWnd
Change - Unless your getting the SendMessage functions return value then remove the parentheses which are the ( and ) characters.
Thats basically the only changes that are needed.
_________________________________
Page Left / Page Right
'
'Move the position pageleft.
SendMessage(txtControl.Handle.ToInt32, WM_HSCROLL, SB_PAGELEFT, 0)
'
'Move the position pageright.
SendMessage(txtControl.Handle.ToInt32, WM_HSCROLL, SB_PAGERIGHT, 0)
Line Left / Line Right
'
'Move the position left.
SendMessage(txtControl.Handle.ToInt32, WM_HSCROLL, SB_LINELEFT, 0)
'
'Move the position right.
SendMessage(txtControl.Handle.ToInt32, WM_HSCROLL, SB_LINERIGHT, 0)
Left Edge / Right Edge
'
'Move the position to the left edge.
SendMessage(txtControl.Handle.ToInt32, WM_HSCROLL, SB_LEFT, 0)
'
'Move the position to the right edge.
SendMessage(txtControl.Handle.ToInt32, WM_HSCROLL, SB_RIGHT, 0)
_________________________________
Thats basically all for Part #1. These 6 features do not have native VB or VB.NET codes and therefore the API's are used. As you can see though its pretty simple to add..
Part #2 will go over: Page Up/Page Down, Line Up/Line Down, and Top Edge/Bottom Edge.
Till then, Have fun!
Jason
Topics: - (.5.0/6.0), - (.NET 05/08 Only), - (.NET All + 05/08), - .All VB (Related to All) | No Comments »


(5 votes, average: 4.6 out of 5)