Free Programming Website

Free Programming Website www.sourcecodehub.com

Tuesday, October 30, 2012

Advanced Textbox Manipulation in Visual Basic and VB.NET | Part 2

This is Part Two of the Advanced Textbox Control Manipulation series. If you haven’t already you should check out Part 1 of this series. That article showed how to use the SendMessage API call to make a Textbox control Page Left, Page Right, Line Left, Line Right, Left Edge, and go to the Right Edge of the contents. This Part 2 post will show how to add the functionality that is outlined below…

Page UP
Page Down
Line UP
Line Down
Top Edge
Bottom Edge

Note: These codes are basically taken from an example I made at my vbcodesource.com site for VB.NET that shows how to do lots and lots (and lots?) of various textbox based manipulating and functionality. Just go to http://www.vbcodesource.com/ under the Visual Basic.NET – Examples page.

_________________________________

To accomplish these features I will use the Windows API for most tasks. Specifically the Send Message Function is used. Below is the declaration for Visual Basic 6.0 (same for VB 5.0 as well) and VB.NET. All versions of .NET is supported, even the latest VB 2005, VB 2008, and Visual Basic 2010 versions. You shouldn’t have to change anything from the DotNet codes in this article.

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, VB 2005, VB 2008 and Visual Basic 2010
'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…

Visual Basic 6.0…
Private Const EM_SCROLL = &HB5
 
Private Const SB_PAGEDOWN = 3
 
Private Const SB_PAGEUP = 2
 
Private Const SB_LINEUP = 0
 
Private Const SB_LINEDOWN = 1
 
Private Const SB_BOTTOM = 7
 
Private Const SB_TOP = 6

Visual Basic.NET through VB 2008 and VB 2010…
Private Const EM_SCROLL As Int32 = &HB5
 
Private Const SB_PAGEDOWN As Int32 = 3
 
Private Const SB_PAGEUP As Int32 = 2
 
Private Const SB_LINEUP As Int32 = 0
 
Private Const SB_LINEDOWN As Int32 = 1
 
Private Const SB_BOTTOM As Int32 = 7
 
Private Const SB_TOP As Int32 = 6

_________________________________

Now all thats really needed is to call the sendmessage api with the right combination of constants to perform the intended function on your textbox control.

_________________________________

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 UP / Page Down
'Move the cursor position up one page.
SendMessage(txtControl.Handle.ToInt32, EM_SCROLL, SB_PAGEUP, 0)
 
 
'Move the cursor position down one page.
SendMessage(txtControl.Handle.ToInt32, EM_SCROLL, SB_PAGEDOWN, 0)

Line UP / Line Down
'Move up by one line.
SendMessage(txtControl.Handle.ToInt32, EM_SCROLL, SB_LINEUP, 0)
 
 
'Move down by one line.
SendMessage(txtControl.Handle.ToInt32, EM_SCROLL, SB_LINEDOWN, 0)

Go to Top Edge / Go to Bottom Edge
'The will make the textbox scroll to the top without moving the cursor.
SendMessage(txtControl.Handle.ToInt32, EM_SCROLL, SB_TOP, 0)
 
 
'This will make the textbox scroll to the bottom without moving the cusor.
SendMessage(txtControl.Handle.ToInt32, EM_SCROLL, SB_BOTTOM, 0)

Below is another method of scrolling the textbox to the Top and Bottom. But this WILL move the caret from its current position. The API method will NOT change the caret position. So using the code below depends on whether your ok with the cursor changing positions or not.

Scroll to Top with Cursor / Scroll to Bottom with Cursor
'
'Scroll the textbox to the Top.
'
'Set the cursor to the first character in the textbox which will be at the top of the
'control.
TextBox1.SelectionStart = 0
'
'Make the textbox scroll to the actually caret postition.
TextBox1.ScrollToCaret
'
'
'Scroll the textbox to the Bottom.
'
'Set the cursor to the last character in the textbox which will be at the bottom of the
'control.
TextBox1.SelectionStart = TextBox1.TextLength
'
'Make the textbox scroll to the actual caret postition.
TextBox1.ScrollToCaret

_________________________________

These 6 features can really add alittle extra control to your custom notepad program, wordpad program, or any program I guess. As you can see its pretty simple to add. Thats all for Part #2. Have fun!