This tutorial in vb.net will show to us how to change password character in runtime or during the execution of the program.
PasswordChar is a property of a textbox control that allows you to set what character are to be displayed in a textbox instead of the actual characters.
This is usually used in the password field where the actual characters being entered are not displayed or exposed.
Let’s start the tutorial.
- Open Microsoft Visual Studio 2010 or higher versions.
- Select File and create a New Project.
- Select Visual Basic, then Windows Form Application.
- We will now add controls to the form.
Sample form layout
- Double click the type password textbox. You will see this code.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub
When you double click the textbox control, the default event opened is the TextChanged. The code placed in this event will be triggered whenever the user has entered something on the text field or textbox.
- Paste the code below in the TextChange event of the textbox.
TextBox1.PasswordChar = TextBox2.Text
The text to be displayed in the password field is equal to the character being set in password character textbox.
Ex. If user sets the password character to *, then the user enters admin in the password field, instead of displaying admin, the password field will display 5 asterisk symbol.
Note: the user has the freedom what character to use as password char. The password field will automatically change the display. The PasswordChar can be changed during the runtime or when the program is running.
- If you really want to know what are the exact characters are being entered, kindly double click the Show password checkbox and paste the code below.
If CheckBox1.Checked = True Then TextBox1.PasswordChar = "" Else TextBox1.PasswordChar = TextBox2.Text End If
This will empty the value of the PasswordChar property of the password field.
Note: setting the PasswordChar to empty, null or blank this will automatically unmasking the field.
- Test the project and don’t forget to save the file.
Hope you have learned something from this tutorial. Thank you and more tutorials to come.