Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InputBox as password character(*)? 12

Status
Not open for further replies.

shanmugham

Programmer
Jun 19, 2001
71
IN
Hello, expert

Is there any way to make the text entered in the InputBox as password character(*)?

thanks in advance

shan
 
Don't think so, though you could design a popup form that looks much like an input box, use a text field, and set its PasswordChar property to *.


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Dear Sir,

i dont want to create a new form,

just input box.... enough for me???

shan
 
Yes it is possible. Place the following into a module:
Code:
Option Explicit

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

Private Declare Function FindWindowEx Lib "user32" Alias 
"FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal 
lpsz1 As String, ByVal lpsz2 As String) As Long

Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, 
ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)

Private Declare Function KillTimer& Lib "user32" (ByVal hwnd&, 
ByVal nIDEvent&)

Private Declare Function SendMessage Lib "user32" Alias 
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal 
wParam As Long, lParam As Any) As Long

Const EM_SETPASSWORDCHAR = &HCC
Public Const NV_INPUTBOX As Long = &H5000&
Public WindowTitle As String

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent 
As Long, ByVal dwTime As Long)
    Dim EditHwnd As Long
    EditHwnd = FindWindowEx(FindWindow("#32770", WindowTitle), 
0, "Edit", "")
    
    Call SendMessage(EditHwnd, EM_SETPASSWORDCHAR, Asc("*"), 0)
    KillTimer hwnd, idEvent
End Sub

Public Function PasswordInputbox(Prompt As String, Optional Title As 
String, Optional Default As String, Optional XPos As Long, Optional YPos 
As Long, Optional HelpFile As Long, Optional Context As Long) As String
        Dim ret As String
    If Title = "" Then
        WindowTitle = App.Title
    Else
        WindowTitle = Title
    End If

    SetTimer 0, 0, 1, AddressOf TimerProc
    PasswordInputbox = InputBox(Prompt, WindowTitle, Default, XPos, YPos, 
HelpFile, Context)
End Function

and call it from a form like:

Code:
Private Sub Command1_Click()
Dim a
a = PasswordInputbox("Enter Text", "Title")
End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
*blinks* That is WAY above my skill level. :)

"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Not a lot of skill required to Copy and Paste the above code into your own project.

It's a good way to learn the VB Language as well.


Greg Palmer

----------------------------------------
Any feed back is appreciated.
 
Public Const NV_INPUTBOX As Long = &H5000&

above line shows error, any need to add ??


shan
 
That one should work fine. However, it needs to be in a module; trying to put this line in a form would throw the error "Constants, (...) not allowed as Public members of object modules". Is that the error you're getting?


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
yes sir, same error

how to overcome this error ... give me in detail

please ... thanks in advance


shan
 
You will have to put this line in a module that isn't also an object. So, not a form module (and I believe not a class module either).

If you don't have a regular module yet, create one, and you can put all constant declarations in there. As long as you declare them as Public like this one, they'll apply to your entire application.


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Actually, the whole code ca8msm shared is best placed into a module, and as he suggested you would then call this code from whatever form you want to use it in with:

Code:
Private Sub Command1_Click()
Dim a
a = PasswordInputbox("Enter Text", "Title")
End Sub

Since PasswordInputbox is a Public sub, it doesn't matter what module you put it in, your form will know where to find it.


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
i put the following lines in to modules

Public Const NV_INPUTBOX As Long = &H5000&
Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)

after that
it shows the error in the following lines

SetTimer 0, 0, 1, AddressOf TimerProc

sorry for the ?????

thanks in advance

shan
 
Did you copy all of the code? Because it should work just fine if you did.

I've uploaded a working example at have a look and compare.


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
yes yes thanks


its working fine,....


thank you very much much sir

shan
 
I really don't understand why you would want to use an InputBox for this, and not just use a text box on a modal form.

If you add a small form to your project with a text box, a lable and two command buttons on it, and name it frmpassword, you can do what you want in two minutes by pasting the below into the new form's code window:

[blue]
Code:
Option Explicit
Private m_sRetValue As String
Public Function GetPassword(Optional Prompt As String, Optional Title As String, Optional XPos As Variant, Optional YPos As Variant) As String
    Me.Caption = Title
    Label1.Caption = Prompt
    Text1.PasswordChar = "*"
    
    If Not IsMissing(XPos) Then Me.Left = XPos
    If Not IsMissing(YPos) Then Me.Top = YPos
    
    Me.Show vbModal
    GetPassword = m_sRetValue
    Unload Me 'DO NOT REMOVE THIS 2nd UNLOAD
End Function
    
Private Sub Command1_Click()
    m_sRetValue = Text1.Text
    Unload Me
End Sub

Private Sub Command2_Click()
    m_sRetValue = vbNullString
    Unload Me
End Sub
[black]

You would call it like this:
ReturnValue = frmpassword.GetPassword("HI","XYZ")



If you need some help with the positioning of the controls, etc, then place this also in the code window (you only need to add a form, two command buttons, a label and a text box, leaving the default names as they are):
Code:
Private Sub Form_Load()
    With Me
        .Height = 2220
        .Width = 5670
        'instead of using Screen here, you could find and use the actual work area
        .Left = (Screen.Width - 5670) \ 2
        .Top = (Screen.Height - 2220) \ 2
    End With
    With Label1
        .Width = 4000
        .Height = 1260
        .Left = 120
        .Top = 130
    End With
    With Text1
        .Width = 5300
        .Height = 285
        .Left = 120
        .Top = 1440
        .TabIndex = 1
        .TabStop = True
        .Text = ""
    End With
    With Command1
        .Width = 1100
        .Height = 375
        .Left = 4300
        .Top = 130
        .Default = True
        .TabIndex = 2
        .TabStop = True
        .Caption = "OK"
    End With
    With Command2
        .Width = 1100
        .Height = 375
        .Left = 4300
        .Top = 600
        .TabIndex = 3
        .TabStop = True
        .Caption = "Cancel"
    End With
End Sub
 

BTW, You should set the form properties: MaxButton and MinButton to false.

This input box can be easily changed as to how it looks, what arguments are passed to it, etc., etc.

If the argument list gets too long, then I suggest using Public Properties and set this prior to calling the GetText or GetPassword function.
 
I just want to thanks ca8msm for a nice work above . . .this is really great



Please pardon the grammar.
Not good in english.
 
ca8msm,
The code works fine - but what does the following line do?
SetTimer 0, 0, 1, AddressOf TimerProc

Removing it makes the characters display regularly instead of as * - but I'm not sure why a timer is required.
 
The timer is required to set the password character after the input box is displayed.

You cannot change the password character using regular code because when the InputBox function is called, the execution of the code is suspended until the user dismiss the dialog box.

Thus a timer is setup before calling the InputBox function. The purpose of this timer is to change the password character of the InputBox edit field after the dialog is displayed. The code is:
[tt]
SetTimer 0, 0, 1, AddressOf TimerProc
[/tt]
The timeout interval of the timer is set to 1 millisecond causing it to fire immediately after the input box is shown. The [tt]AddressOf TimerProc[/tt] argument passes the address of the TimerProc callback function to the SetTimer function so that the timer executes this code after each timeout interval.

When the TimerProc function executes for the first time, it does the following.
1. Finds the handle of the InputBox dialog (FindWindow).
2. Finds the handle of the Edit field inside the InputBox (FindWindowEx).
3. Changes the password character (SendMessage).
4. Kills the timer to prevent further executions of the callback function (KillTimer).

If you comment or remove the SetTimer function call, all of the above actions are inhibited thus leaving the password textbox intact.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top