# VB InputBox() Question



## html81993 (Jul 8, 2005)

I am writing an app that requires a password. Using an input box, is there anyway to hide the password?

With regular textboxes, you can set a PasswordChar...Can this be done with the InputBox() function?


----------



## jdean (Jan 20, 2002)

Don't think so. InputBox is not meant to be a full-featured user interface, it's just there for quick-and-dirty coding.


----------



## Scott786 (Jul 10, 2008)

According to:http://www.vbforfree.com/?p=364 and http://www.tek-tips.com/viewthread.cfm?qid=662729 you can actually do it through API programming. While this is confusing it works. If the reason you want to use the input box is because it captures control from the form then just use the show dialog method.


----------



## html81993 (Jul 8, 2005)

Thanks. I will try those out.


----------



## Zack Barresse (Jul 25, 2004)

There isn't a Password Character property? What version do you have?


----------



## Rollin_Again (Sep 4, 2003)

Instead of using an inputbox why don't you just create your own custom form? You can mask the typed text using a regular textbox on the form.

Regards,
Rollin


----------



## Zack Barresse (Jul 25, 2004)

Oh right, that's what I was thinking of... duh! Sorry 'bout that..


----------



## html81993 (Jul 8, 2005)

I am using both SharpDevelop 2.2 (runs from my flash drive) and Visual Studio Express Edition 2008 (from my hard drive)


----------



## Zack Barresse (Jul 25, 2004)

VS express should have the property of a text box control.


----------



## html81993 (Jul 8, 2005)

It does. Just not for the InputBox() function.


----------



## Zack Barresse (Jul 25, 2004)

And of the other two solutions provided? Have you tried those?


----------



## html81993 (Jul 8, 2005)

No. I am just going to use a second form, as mentioned here. The API calls seem too difficult.


----------



## html81993 (Jul 8, 2005)

Sorry it's been a while. I tried both of those. None of them worked. I need the VB.NET code, those appear to be VB6.0.


----------



## Scott786 (Jul 10, 2008)

Weren't you going to create a second form?

Anyway the first example at least works for dot net. I just tried it and it works. paste this in to you form which has a button called btnDisplay and a label called lblPw.

```
Public Class Form1

    '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
    '
    '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 = 42
    '
    'Used by the SendMessage API to tell the inputbox editbox (textbox) to display the input as password characters.
    Public Const EM_SETPASSWORDCHAR = &HCC

    'Used to have the API call to look for a child based window.
    Public Const GW_CHILD = 5
    '
    'The message for when a new window is created.
    Public Const WM_CREATE = &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.

    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:"
        '
```
You can change what value you want returnPW to be and also what the default response should be

```
returnedPW = InputBox("Enter the Password in the box!", inputTitle, [COLOR="Red"]"password!"[/COLOR], 0)
     If Not returnedPW = Nothing AndAlso [COLOR="red"]returnedPW = "password!"[/COLOR] Then
            lblPw.Text = "InputBox Password is: " & returnedPW
        Else
            lblPw.Text = "InputBox Password is: Wrong!"
            MessageBox.Show("Wrong Password!", " Try Again")
        End If
    End Sub
    '
    '
    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
    '
    '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

End Class
```


----------



## html81993 (Jul 8, 2005)

I was going to use a second form, but decided against it. I probably should have clarified that. I will try your code though. Thanks.


----------



## Scott786 (Jul 10, 2008)

That's fine 

Just a quick correction though. It's not my code. I fixed up the code from http://www.vbforfree.com/?p=364 as I posted above. I just made it a copy and paste job.


----------



## html81993 (Jul 8, 2005)

I get the following error:



> Handles clause requires a WithEvents variable defined in the containing type or one of its base types. (BC30506)


Also, the textbox still shows everything...


----------



## Scott786 (Jul 10, 2008)

I dunno... I copied the code I posted and pasted it one after the other. It compiled and worked. Are you using vista or XP? I am using Vista and it worked. Try not to edit anything. Just create a form with a button whose name is btnDisplay and and a label called lblpw. Make sure you are editing the name value not the text.

Here is the code again but unhampered by the break.


```
Public Class Form1

    '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
    '
    '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 = 42
    '
    'Used by the SendMessage API to tell the inputbox editbox (textbox) to display the input as password characters.
    Public Const EM_SETPASSWORDCHAR = &HCC

    'Used to have the API call to look for a child based window.
    Public Const GW_CHILD = 5
    '
    'The message for when a new window is created.
    Public Const WM_CREATE = &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.

    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
    '
    '
    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
    '
    '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

End Class
```
Sorry if it seems I am telling you that you made a mistake but that is a bit more likely than it not working cause of that error. Make it in VB 2008, don't try to edit it at all and name the buttons and labels properly.


----------



## html81993 (Jul 8, 2005)

I am using SharpDevelop 2.2, but I also did try VB.Net Express 2008 and it did work there. Is there anyway to import the .exe that works in Express 2008 into SharpDevelop?


----------

