Tag Archives: line left

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

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (20 votes, average: 3.95 out of 5)
 

This is the first post in a two part series on how to do add some extra textbox features not exposed in your normal VB or VB.NET textbox. For instance, Page Left/Right and Line Up/Down.

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 of various textbox based manipulating. Just go to http://www.vbcodesource.com/ under the Visual Basic.NET – Examples page.

So in Part #1 I will provide code to add six new abilities 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.


First is the API function

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, 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, VB 2010, and newer


    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


All that’s really needed is the Textbox you want to manipulate and call Sendmessage with the right combination of constants to perform the intended function.

NOTE: If your using VB 6.0 then change one thing in the rest of the code below…

  • Instead of txtControl.Handle.ToInt32, make it txtControl.hWnd

That’s basically the only change 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

As you can see these non-native features were simple to add and configure.

Part #2 will go over:

  1. Page Up
  2. Page Down
  3. Line Up
  4. Line Down
  5. Top Edge
  6. Bottom Edge

Well that’s basically it for this article, Have fun!

              Jason

Revised: 2014