• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

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?
0
jsctechy
Asked:
jsctechy
  • 12
  • 12
  • 4
  • +1
1 Solution
 
appariCommented:
with the default inputbox its not possible. create your own form and you can make the textbox to mask the password.
0
 
ArkCommented:
'===========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")

0
 
jsctechyAuthor Commented:
Ark:
how do i use you code?

should i just copy and paste on the sub i'm working?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jsctechyAuthor Commented:
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
0
 
ArkCommented:
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")
0
 
ArkCommented:
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
0
 
jsctechyAuthor Commented:
ok i will waiting...........
0
 
ArkCommented:
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
0
 
jkaiosCommented:
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 ]==============================
0
 
jsctechyAuthor Commented:
i just got two little errors on the module code:

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

Mircosoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-42)
0
 
jkaiosCommented:
Oh sorry for the inconvenience ... thought you're using VB6.
0
 
jsctechyAuthor Commented:
anyhting on this?
0
 
ArkCommented:
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.
0
 
jsctechyAuthor Commented:
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.
0
 
ArkCommented:
I haven't "Any" and "App.Title" in my code (code = ID: 19643375 - it's above code you'r speaking about)
0
 
jsctechyAuthor Commented:
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?
0
 
ArkCommented:

    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
0
 
jsctechyAuthor Commented:
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 *********
0
 
ArkCommented:
'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 = ""
0
 
jsctechyAuthor Commented:
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?
0
 
ArkCommented:
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
0
 
jsctechyAuthor Commented:
i have replace my code with your and when the second message box shows and i enter the input it does not shows ********
0
 
jkaiosCommented:
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:")
0
 
ArkCommented:
>>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
0
 
ArkCommented:
Smth like this - add dialog window(Dialog1) to you project, place 3 labels (lblPrompt, lblPassword, lblConfirm), 2 textboxes (txtPassword,txtConfirm) and 2 buttons (cmdOk, cmdCancel) on form:

Public Class Dialog1
    Public oldPassword, newPassword As String
    Dim bOKPressed As Boolean

    Private Sub Dialog1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If bOKPressed Then
            e.Cancel = Not ValidateInput()
        End If
    End Sub

    Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Change Password"
        lblPrompt.Text = "For Security Reasons you are ask to change your password" & vbCrLf & "Please enter your new desired password and confirm it:"
        lblPassword.Text = "New password:"
        lblConfirm.Text = "Confirm password:"
        cmdOK.Text = "&OK"
        cmdOK.DialogResult = Windows.Forms.DialogResult.OK
        cmdCancel.Text = "&Cancel"
        txtPassword.PasswordChar = "*"
        txtConfirm.PasswordChar = "*"
        bOKPressed = False
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        bOKPressed = True
    End Sub

    Private Function ValidateInput() As Boolean
        If txtPassword.Text = "" Then
            MsgBox("Password field empty!")
            txtPassword.Focus()
            Return False
        End If
        If txtConfirm.Text = "" Then
            MsgBox("Confirmation field empty!")
            txtConfirm.Focus()
            Return False
        End If
        If txtPassword.Text = oldPassword Then
            MsgBox("You can not use the previous password", MsgBoxStyle.Critical, "Entry Error")
            txtPassword.Clear()
            txtPassword.Focus()
            Return False
        End If
        If txtConfirm.Text = txtPassword.Text Then
            MsgBox("The password Enter Does not Match your previous entry. Try Again!", MsgBoxStyle.Critical, "Wrong Password")
            txtConfirm.Clear()
            txtConfirm.Focus()
            Return False
        End If
        newPassword = txtPassword.Text
        Return True
    End Function
End Class
0
 
jsctechyAuthor Commented:
what type of dialog i have to add?

under dialogs i have:
Pointer
ColorDialog
foldebrowserDialog
fontDialog
openfiledialog
savefiledialog

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 12
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now