Tag Archives: Findwindow

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

Auto Complete Support in Textbox and Combobox Controls using VB 6.0 and VB.NET

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

Autocomplete is a very useful feature that Microsoft added with the release of Internet Explorer 5.0 and therefore Internet Explorer 5.0 or higher needs to be installed on the target computer. Adding autocomplete to your applications can be a real benefit to the user and is a easy feature to implement.

Note: Visual Basic 2005 and Visual Basic 2008 already has Autocomplete capabilities in the Textbox and Combobox controls by default.


There are a few different locations you can get the autocomplete data from…

  • File System
  • History List
  • URLs Recently Used
  • ALL URLs
  • ALL System Sources

There are likewise a few different options available to set the way you want AutoComplete to react.

  • Append
  • Suggest
  • Append & Suggest

Visual Basic.NET

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Int32, ByVal _
        completeFlags As Int32)

Visual Basic 6.0

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Long, ByVal _
        completeFlags As Long)

The SHAutoComplete API Sub is easy to use. The first parameter is the Handle to the Edit (Combobox/Textbox) control you want to enable Auto Complete too. The second Parameter is how you want your Autocomplete to react and display.


 

    '
    'Various Flags you can set to be the source.
    '
    Private Const Defaults = &H0

    Private Const Filesystem = &H1

    Private Const HistoryList = &H2

    Private Const URLRecentlyUsedList = &H4

    Private Const AllURLs = (HistoryList Or URLRecentlyUsedList)

    Private Const AllSystemsSources = AllURLs Or FileSystem

The flags below are used to specify how you want your autocomplete feature to react.

    '
    'Flags you can use to specify how your want autocomplete to react.
    '
    Private Const append = &H40000000 Or &H20000000

    Private Const suggest = &H10000000 Or &H80000000

    Private Const suggestAppend = &H40000000 Or &H10000000

Now that the API based codes are out of the way, you can setup the edit based controls (Like Textbox and Combobox) to have Autocomplete capabilities. Below is a simple example of adding autocomplete to a textbox control that I named txt.

        'Set autocomplete to the textbox using the system defaults.
        SHAutoComplete(txt.hWnd, Defaults)

        'You can also specify how it will react and what it will display.
        SHAutoComplete(txt.hWnd, suggest Or AllURLs)

If your using .NET, just change txt.hWnd to txt.Handle.ToInt32.

Unfortunately adding this feature to a Combobox isn’t as straight forward. Thats because when you use the Handle/Hwnd Property for the Combobox it returns the Main Window Portion of the control and NOT the Edit Window portion. There are a couple ways to get around this though. One way it to use the GetComboBoxInfo API or to use the FindWindowEx API call. I chose to go ahead and use the FindWindowEx API.


Visual Basic.NET

    '
    'API Function used to get the Edit window of a combobox control.
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Int32, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

Visual Basic 6.0

    '
    'API Function used to get the Edit window of a combobox control.
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal handle As _
        Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Now you just need to use the FindWindowEx Function using the 1st and 3rd parameter. And once you call the FindWindow function, simply pass its return value to the SHAutoComplete Method.

        '
        'Simply change this variable type to a Integer if your using VB.NET.
        Dim cmbHandle As Long

        'Retrieve the handle to the ‘Edit’ Window of the combobox. I named my Combobox control cmb.
        cmbHandle = FindWindowEx(cmb.hWnd, 0, "Edit", vbNullString)

        'Activate Autocomplete to the combobox edit window.
        SHAutoComplete(cmb.hWnd, Defaults)

Tip: Here is how you can specify exactly how to react and display….

        '
        'You can also specify how it will react and what it will display.
        SHAutoComplete(cmb.hWnd, suggest Or AllURLs)

Again, change cmb.hWnd to cmb.Handle.ToInt32 if your using Visual Basic.NET.

Thats all there is to it! Depending on your application, you or your apps user will really appreciate you putting that feature in your program. Well, I guess there is nothing more to add. Have fun!

Jason

How to Autocomplete ComboBoxes like Textboxes using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

I’ve seen this question before and the answer is pretty simple when you get an understanding of the combo control. When you use the handle associated with the Combobox, it is the main portion of the Control that the handle refers to and NOT the edit box which is the important part.

Note: IF your using Classic Visual Basic (5.0 or 6.0) then simply change the int32 and Integer variable types to Long types since the codes here are setup for VB.NET.


To get the handle to this area of the Combobox will require a windows api call. The API Call is: FindWindowEx. This call will get the Handle of the specified control according to the specified Class Name.

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

The parameters we are interested in is hWnd and lpszClass. You can set the others to Null. The Handle would be the handle to the main portion of a control. Which is what the ComboBox.Handle.ToInt32 method returns. The class name is the class type you want to get the handle too. Which happens to be “Edit”

        'Will hold the “Edit” handle given by the FindWindowEx API call from the ComboBox.
        Dim cmbHandle As Int32 = FindWindowEx(ComboBox.Handle.ToInt32, 0, "Edit", vbNullString)

And thats all you need to make the combobox Autocomplete capable. Simply specify the cmbHandle integer in the SHAutoComplete ‘handle’ parameter and it will give your combobox the Autocomplete feature!

More Info: If you want the complete source code to autocomplete combobox controls and textbox controls with the actual SHAutoComplete source codes as well, then simply click on this link to view the post.

               Jason

Revised: 2015