Visual Basic .Net How to Change Password Character in Runtime

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.

  1. Open Microsoft Visual Studio 2010 or higher versions.
  2. Select File and create a New Project.
  3. Select Visual Basic, then Windows Form Application.
  4. We will now add controls to the form.

2 labels
2 textboxes
1 checkbox

Sample form layout

vbnetpasschar1

  1. 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

Code Explanation:

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.

  1. Paste the code below in the TextChange event of the textbox.
TextBox1.PasswordChar = TextBox2.Text

Code Explanation:

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.

  1. 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

Code Explanation:

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.

  1. Test the project and don’t forget to save the file.

vbnetpasschar2

Hope you have learned something from this tutorial. Thank you and more tutorials to come.

Download

, , , ,

Post navigation