Link to home
Start Free TrialLog in
Avatar of jsctechy
jsctechyFlag for United States of America

asked on

Input box display user entry password like (use *** instead of regular strings)

hi there,
I have an input box that needs to collect from the user a new password, but as sensitite as it can be i dont want to display what the user is entering on the box instead of the entry i want to display ******* as the user type it.
here is the input box declaration:
 Newpassword = InputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

can this be done?
Avatar of appari
appari
Flag of India image

with the default inputbox its not possible. create your own form and you can make the textbox to mask the password.
Avatar of Ark
'===========Bas 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
Private 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
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const EM_SETPASSWORDCHAR = &HCC
Const NV_INPUTBOX As Long = &H5000&
Dim sTitle As String

Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
    Dim EditHwnd As Long
    EditHwnd = FindWindowEx(FindWindow("#32770", sTitle), 0, "Edit", vbNullString)
    If EditHwnd Then
       Sleep 10
       Call SendMessage(EditHwnd, EM_SETPASSWORDCHAR, Asc("*"), 0)
       KillTimer hwnd, idEvent
    End If
End Sub

Public Function PasswordInputBox(Prompt, Optional Title, Optional Default, Optional XPos, Optional YPos, Optional HelpFile, Optional Context) As String
    If IsMissing(Title) Then
       sTitle = App.Title
    Else
       sTitle = Title
    End If
    SetTimer 0&, NV_INPUTBOX, 5, AddressOf TimerProc
    PasswordInputBox = InputBox(Prompt, Title, Default, XPos, YPos, HelpFile, Context)
End Function

'==========Using============
Newpassword = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

Avatar of jsctechy

ASKER

Ark:
how do i use you code?

should i just copy and paste on the sub i'm working?
if that the way i have to go this does not work because the las sub in you code throw some errors.
Such as:
1.Public Function PasswordInputBox(Prompt, Optional Title, <== optional Parameters must specify a default value

2.IsMissing is not declared

3.Addressof <== expersion cannot be converted to long becasue 'long' is not a delegated type

4. Default <== Expression Expected
Add new bas module to your project and add my code above (or, if you already have bas module in your app, just add my code). Then when you need just call

Newpassword = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")
Oops, seems you're using VB.Net? This was not cpecify in your question. Wait a moment, I'll rewrite code for VB.Net using
ok i will waiting...........
This took a while....
VB.Net controls have their own class names

'============Module============
'Choose Project-> add module from menu
Module Module1
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Delegate Function EnumWindProc(ByVal hWnd As Int32, ByVal lParam As Int32) As Boolean
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWnd As IntPtr, ByVal lpEnumFunc As EnumWindProc, ByRef lParam As IntPtr) As Int32
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    Const EM_SETPASSWORDCHAR As Integer = &HCC&
    Dim sTitle As String
    Dim editHwnd As Integer
    Dim tmr As System.Threading.Timer

    Sub TimerProc(ByVal stateInfo As Object)
        Dim inputBoxHwnd As IntPtr = FindWindow(vbNullString, sTitle)
        GetIBEditControl(inputBoxHwnd)
        If editHwnd Then
            System.Threading.Thread.Sleep(10)
            Call SendMessage(editHwnd, EM_SETPASSWORDCHAR, Asc("*"), 0)
            tmr.Dispose()
        End If
    End Sub

    Public Function PasswordInputBox(ByVal Prompt As String, Optional ByVal Title As String = "", Optional ByVal DefaultResponse As String = "", Optional ByVal XPos As Integer = -1, Optional ByVal YPos As Integer = -1) As String
        If Title = "" Then
            sTitle = My.Application.Info.Title
        Else
            sTitle = Title
        End If

        Dim autoEvent As New System.Threading.AutoResetEvent(False)
        tmr = New System.Threading.Timer(AddressOf TimerProc, autoEvent, 5, 10)
        PasswordInputBox = Microsoft.VisualBasic.Interaction.InputBox(Prompt, Title, DefaultResponse, XPos, YPos)
    End Function

    Private Function EnumWindowsProcess(ByVal hwnd As Int32, ByVal lParam As Int32) As Boolean
        Dim sb As New System.Text.StringBuilder("", 256)
        GetClassName(hwnd, sb, sb.Capacity)
        Dim className As String = sb.ToString
        If className.ToUpper.Contains("EDIT") Then
            editHwnd = hwnd
            Return False
        End If
        Return True
    End Function

    Private Sub GetIBEditControl(ByVal hwnd As IntPtr)
        EnumChildWindows(hwnd, AddressOf EnumWindowsProcess, 0)
    End Sub
End Module

'==========using from form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Newpassword As String = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")
    End Sub
Try this:

NOTE: DO NOT specify "title" parameter of the InputBox() function.  Doing so will NOT mask the input.


'=========================[ 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
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
Private Declare Function KillTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&)
Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)
Public Const NV_INPUTBOX As Long = &H5000&
Private Const EM_SETPASSWORDCHAR = &HCC

Public Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)

   Dim inputHwnd As Long
   
   ' Find a handle to the InputBox window, then to the textbox
   ' the user types in (known as "Edit")
   inputHwnd = FindWindowEx(FindWindow("#32770", App.Title), 0, "Edit", "")
   
   ' Send masked chars to the target InputBox as the user types.
   ' The masked chars in this sample is the Asc("*") - the "*"
   ' can be changed to whatever you like.
   Call SendMessage(inputHwnd, EM_SETPASSWORDCHAR, Asc("*"), 0)
   
   ' Destroy the timer object when done (user clicks OK or Cancel on the InputBox).
   KillTimer hwnd, idEvent

End Sub
'=========================[ Module Code ]=============================

'==========================[ Form Code ]==============================
Private Sub Command1_Click()

   Dim sInput As String
   SetTimer hwnd, NV_INPUTBOX, 10, AddressOf TimerProc
   sInput = InputBox("Enter Password:")    '<-- DO NOT SPECIFY THE TITLE, JUST THE PROMPT.

   MsgBox "Password: " & sInput

End Sub
'==========================[ Form Code ]==============================
i just got two little errors on the module code:

Any <== is not supported in 'Declare Statements.
App.Title <== Name 'App' is not declared.
Are you using VB.net or VB6?
I'm using Microsoft Visual Studio 2005 Profesional Edition.

Mircosoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-42)
Oh sorry for the inconvenience ... thought you're using VB6.
anyhting on this?
Did you try my last sample - it's for VB net. Start new project, place button on form, Choose Project-> add module from menu, copy and paste module code inside module (excluding first line "Module Module1" and last line "End Module" - these lines VS add automatically). Run project and press button.
yeah and i get this:

two little errors on the module code:

Any <== is not supported in 'Declare Statements.
App.Title <== Name 'App' is not declared.
I haven't "Any" and "App.Title" in my code (code = ID: 19643375 - it's above code you'r speaking about)
I'm sorry your code does work.

if  i need to have a confirmation input box can i just use it like this:

ConfirmPassword = PasswordInputBox("Please confirm your password: ", "Change Password")

if it is isn't working?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ConfirmPassword As String = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")
        MsgBox(ConfirmPassword)
    End Sub
Actually that wasn't what i have in mind what i'm trying to do is this:

Private Sub PasswordReset(ByVal currentpassword As String, ByVal UserName As String)

        Dim Newpassword As String
       
        Newpassword = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

        If Trim(Newpassword) = currentpassword Then
            Do
                MsgBox("You can not use the previous password", MsgBoxStyle.Critical, "Entry Error")
                PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

            Loop Until Trim(Newpassword) <> currentpassword Or Newpassword = ""

        Else

            Dim ConfirmPassword = PasswordInputBox("Please confirm your password: ", "Change Password")

            If Trim(ConfirmPassword) <> Trim(Newpassword) Then
                Do
                    MsgBox("The password Enter Does not Match your previous entry. Try Again!", MsgBoxStyle.Critical, "Wrong Password")
                    ConfirmPassword = PasswordInputBox("Please confirm your password: ", "Change Password")
                Loop Until Trim(ConfirmPassword) = Trim(Newpassword)

            Else

                Try

                    Dim params As New ArrayList

                    params.Add(New SqlClient.SqlParameter("@pwd", ConfirmPassword))
                    params.Add(New SqlClient.SqlParameter("@LogonID", UserName))

                    com.library.data.Tools.Execute("usp_UpdateUSerInfo", "usp_UpdateUSerInfo", params)

                    MsgBox("Your New Password has been Changed Sucesfully!")

                Catch ex As Exception
                    Throw ex
                End Try

            End If
        End If


    End Sub


So when the confirm displays i also want to show the input box with *********
'Should be (Added NewPassword=)
               
               MsgBox("You can not use the previous password", MsgBoxStyle.Critical, "Entry Error")
               NewPassword = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

            Loop Until Trim(Newpassword) <> currentpassword Or Newpassword = ""
from the code you see on my previous post the only part that works is when the user is prompt to enter for the new password for the first time other than that it does not work not even on the confirm nor on the second time when the wron password is enter.......
how can i solve this issue?
This code working OK for me:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PasswordReset("currentPassword", "userName")
    End Sub

    Private Sub PasswordReset(ByVal currentpassword As String, ByVal UserName As String)

        Dim Newpassword As String = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

        If Trim(Newpassword) = currentpassword Then
            Do
                MsgBox("You can not use the previous password", MsgBoxStyle.Critical, "Entry Error")
                Newpassword = PasswordInputBox("For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password:", "Change Password")

            Loop Until Trim(Newpassword) <> currentpassword Or Newpassword = ""

        Else

            Dim ConfirmPassword = PasswordInputBox("Please confirm your password: ", "Change Password")

            If Trim(ConfirmPassword) <> Trim(Newpassword) Then
                Do
                    MsgBox("The password Enter Does not Match your previous entry. Try Again!", MsgBoxStyle.Critical, "Wrong Password")
                    ConfirmPassword = PasswordInputBox("Please confirm your password: ", "Change Password")
                Loop Until Trim(ConfirmPassword) = Trim(Newpassword)

            Else
                MsgBox("User Name: " + UserName + vbCrLf + _
                       "Old password: " + currentpassword + vbCrLf + _
                       "New password: " + Newpassword + vbCrLf + _
                       "Confirmed password: " + ConfirmPassword)
            End If
        End If
    End Sub

'PS Add Option Compare Text on the top of your form code make passwords comparing Case insencetive
i have replace my code with your and when the second message box shows and i enter the input it does not shows ********
Try omitting the Title argument (second parameter), but only the prompt as follows:

CHANGE:
   Dim ConfirmPassword = PasswordInputBox("Please confirm your password: ", "Change Password")

TO:
   Dim ConfirmPassword = PasswordInputBox("Please confirm your password:")
>>Try omitting the Title argument (second parameter), but only the prompt as follows:<<
Won't help. Instead, it can make it even worse, if any form.text=ApplicationTitle (usualy mainForm does) first textbox on this form will show asteriks instead of letters.
BTW, jsctechy in your case, IMHO, custom dialog box with 2 textboxes, 2 buttons and 1 label will suit you better - you can validate both input on cmdOK_Click event, show msgbox, clear appropriate field and set focus on this text field
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what type of dialog i have to add?

under dialogs i have:
Pointer
ColorDialog
foldebrowserDialog
fontDialog
openfiledialog
savefiledialog

Which of those should i pick?
Thanks for points, glad I could help
Choose Project from vbNet meny->Add New Iten-> choose "DIalog" fro Visual studio installed template