Tag Archives: sendmessage

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

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

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

Continue reading

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

Textbox Scroll to Bottom or Top – Part 2 using VB 6.0 and VB.NET

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


I have a previous post on how to scroll the textbox controls contents to the bottom and the top that many visitors didn’t appear to like. It has a 1.5 rating out of 10 so, it is not liked very much. I really don’t know why because the code seems to work fine. Maybe because the cursor position would be moved? This new code won’t move the actual cursor if thats the case. But anyways, I decided to post another way to scroll the textbox contents by using the Windows API. This method uses the SendMEssage function and passes a couple constants to perform the up and down scrolling process. Below are the codes for VB 6.0 and VB.NET.

_____________________________________

Visual Basic 6.0 Declarations…

    '
    'Used to Send Messages to the control and will be used with a request for the current line number in the textbox 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
    '
    'Constants used for the SendMessage API call function.
    '
    Private Const EM_SCROLL = &HB5
    Private Const SB_BOTTOM = 7
    Private Const SB_TOP = 6

————————————————

   

   

Visual Basic .NET Declarations…

    '
    'Used to Send Messages to the control and will be used with a request for the current line number in the textbox 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
    '
    'Constants used for the SendMessage API call function.
    '
    Private Const EM_SCROLL = &HB5
    Private Const SB_BOTTOM = 7
    Private Const SB_TOP = 6

————————————-

Now all thats left is to setup and call the SendMessage API function…

The first parameter of the SendMessage call is the handle to your textbox control that you want to use.

The second parameter is to let the api know we want to perform some type of scroll action on the control.

The third parameter is the type of scroll to perform on the control.

The fourth parameter is Not used in this example.

————————————-

Visual Basic 5.0, 6.0 – Scroll the Textbox to the Bottom/Top…

        '
        'This will make the textbox scroll to the bottom without moving the cusor position.
        SendMessage TextBox1.hWnd, EM_SCROLL, SB_BOTTOM, 0
        '
        'The will make the textbox scroll to the top without moving the cursor.
        SendMessage TextBox1.hWnd, EM_SCROLL, SB_TOP, 0

Visual Basic.NET – Scroll the Textbox to the Bottom/Top…

 

        '
        'This will make the textbox scroll to the bottom without moving the cusor.
        SendMessage(TextBox1.Handle.ToInt32, EM_SCROLL, SB_BOTTOM, 0)
        '
        'The will make the textbox scroll to the top without moving the cursor.
        SendMessage(TextBox1.Handle.ToInt32, EM_SCROLL, SB_TOP, 0)

 

_____________________________________

Alright, those codes above should have moved your textbox control contents down to the bottom and up to the top. Also the cursors position will Not be moved like the other method would do. Hopefully this post will be liked alittle better. If not, then I don’t know what else you want and give up… 🙂

Jason

How to Set an Inputbox to Show Password Protected Characters using VB.NET and Higher

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

I made a previous article on password protecting an application and also using Inputboxes to get the username and password. A comment was left on how to make the Inputbox show password characters instead of the actual password being inserted. Well, that is what this article is for, to show how to use .NET and set the Inputbox (Modal Window) to display the input in its textbox as password based characters like asterisks or another char instead of the actual password. Its also called masking the characters in the inputbox. This article will mask the inputbox using Asterisks. But you can set whatever you want the characters to be.


NOTE: While the code in this article is for Visual Basic.NET, VB 2008, and VB.NET 2010, the same principals apply to classic VB as well. You want to Subclass, checking for WM_Create messages. When a WM_Create message is activated, you want to use the Findwindow API to determine whether the newly created window matches the title of the Inputbox you want to show. If it is use GetWindow to get the handle to the Child window which is the edit/text box. Then use the SendMessage API to tell the first ChildWindow in the Inputbox to display all input characters as password characters like asterisks.

Update note 02/27/2011: Steve mentioned that the API call FindWindow may not format correctly when you use Auto in declaring it. So you may have to change Auto to “Ansi” instead. For example: Private Declare Ansi Function FindWindow…..

Instead of Auto or Unicode. The alias name in the api call ends with an ‘A’ which denotes Ansi. If it ended with ‘W’ then it would be Unicode based.

If you run into that problem, use this Declare instead of the one later in the article.


    Private Declare Ansi Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32


AS outlined in a note above, the way I created a password only inputbox was to Subclass and monitor all the WM_Create messages being sent. When the application is notified that a new window was created, it will use the FindWindow API to check for the title of the newly created window. If the title of the window matches the title of the inputbox to be, it will return the handle to that window and call the GetWindow API function to get a handle to the first Child window, which with the Inputbox modal, is the Inputs Edit Box (Textbox) that you type the characters in. Once a handle to the Edit Box is taken, then a call to the SendMessage function to send a EM_SETPASSWORDCHAR message which will make the textbox display password characters which in this article tells the API to use Asterisks characters as the displayed password. You can make it display pretty much any character you want it to. That’s all I did!


    'If you used the Declare Ansi Function FindWindow version above, then ignore this one.
    'Will be used to get the window handles.
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32

Now for some more api calls and variables.

    'Using it to get a handle to the child window of the inputbox.
    Private Declare Function GetWindow Lib "user32"? (ByVal hwnd As Int32, ByVal wCmd As Int32) As Int32

    'It tells the editbox (textbox) to display the contents/input as password characters.
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

    'This will be the title of the Inputbox you will be throwing.
    'This is important since this is the key factor that the example looks for to identify the Inputbox.
    Dim inputTitle As String = "Enter the Password!"

    'Will hold the password returned by the inputbox message.
    Dim returnedPW As String

    'Will be used to code the password characters to appear as Asterisk characters.
    'You can actually have the password text display pretty much any character if you want too.
    'Just change the value of the const to the keycode character you want to use.
    Const asteriskKeyCode As Int32 = 42

    'Used by the SendMessage API to tell the inputbox editbox (textbox) to display the input as password characters.
    Public Const EM_SETPASSWORDCHAR As Int32 = &HCC

    'The message for when a new window is created.
    Public Const WM_CREATE As Int32 = &H1

    'Will hold the return value when calling the FindWindow function which will be the handle to the window.
    Dim windowHandle As Int32

    'Used to have the API call to look for a child based window.
    Public Const GW_CHILD As Int32 = 5

Now that the APIs and Variables are setup, its time for the rest of the codes.

This is the code I put in a button control.

    'This is a Button control I put some of the codes in…
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        '
        'Simply display the inputbox with the title specified in the inputTitle variable and get the value that was put in the edit box.
        '
        'IMPORTANT: The Inputbox is required to have the same title as the window title passed to the FindWindow function. You can try using the Classname if you want to I guess.
        '
        'Clear the previous correct password even if one wasn’t selected.

        lblPw.Text = "InputBox Password:"

        returnedPW = InputBox("Enter the Password in the box!", inputTitle, "password!", 0)

        If Not returnedPW = Nothing AndAlso returnedPW = "password!" Then

            lblPw.Text = "InputBox Password is: " & returnedPW

        Else

            lblPw.Text = "InputBox Password is: Wrong!"

            MessageBox.Show("Wrong Password!", " Try Again")

        End If

    End Sub

This will set the password if the subclass method said their is a inputbox matching the one we want.

    Public Sub setPassword()

        'The codes below will setup the Inputbox Textbox (Edit Window) to be a password edit box and use the asterisk character as the display character.
        Dim editWindow As Int32

        'The Edit Window (Textbox) in the Inputbox is the first Child in the Inputbox Z Order. So just simply tell the API to get the handle to the first child window.
        editWindow = GetWindow(windowHandle, GW_CHILD)

        SendMessage(editWindow, EM_SETPASSWORDCHAR, asteriskKeyCode, 0)

    End Sub

Now for the subclass based procedure.

    '
    'This is used for the actual Subclassing to monitor for Created_Windows and will check to see if a Window matching the title of the inputbox we want.
    '
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        'Have the base class handle all messages.
        MyBase.WndProc(m)

        'Check if a new window has been created.
        If m.Result.ToInt32 = WM_CREATE Then

            'Check the new window to see if it has the charateristics that we are looking for. Namely that is has the Title that we want to find.
            windowHandle = FindWindow(vbNullString, inputTitle)

            'Make sure that the WindowHandle is not 0 which should mean it have a handle to the Inputbox window that we are wanting.
        ElseIf windowHandle > 0 Then

            'Call the code to set the textbox to a password textbox.
            setPassword()

            'Reset the handle back to 0 since the window handle will be different each time you display Inputbox window.
            windowHandle = 0

        End If

    End Sub

That’s all! As is usually the case, with my articles, there is a example available that show how to do this. Simply head over to my http://www.vbcodesource.com/ site and look under the Visual Basic.NET – Examples. Anyways, have fun!

Jason

Revised: 2015