Creating a Password Text Box with the Windows Forms TextBox Control
This documentation is archived and is not being maintained.

Creating a Password Text Box with the Windows Forms TextBox Control

Visual Studio .NET 2003

A password box is a Windows Forms text box that displays placeholder characters while a user types a string.

To create a password text box

  1. Set the PasswordChar property of the TextBox control to a specific character.

    The PasswordChar property specifies the character displayed in the text box. For example, if you want asterisks displayed in the password box, specify * for the PasswordChar property in the Properties window. Then, regardless of what character a user types in the text box, an asterisk is displayed.

  2. (Optional) Set the MaxLength property. The property determines how many characters can be typed in the text box. If the maximum length is exceeded, the system emits a beep and the text box does not accept any more characters. Note that you may not wish to do this as the maximum length of a password may be of use to hackers who are trying to guess the password.

The code below initializes a text box that will accept a string up to 14 characters long and display asterisks in place of the string. The InitializeMyControl procedure will not execute automatically; it must be called.

Security Note   Using the PasswordChar property on a text box can help ensure that other people will not be able to determine a user's password if they observe the user entering it. This security measure does not cover any sort of storage or transmission of the password that can occur due to your application logic. Because the text entered is not encrypted in any way, you should treat it as you would any other confidential data. Even though it does not appear as such, the password is still being treated as a plain-text string (unless you have implemented some additional security measure).
' Visual Basic
Private Sub InitializeMyControl()
   ' Set to no text.
   TextBox1.Text = ""
   ' The password character is an asterisk.
   TextBox1.PasswordChar = "*"
   ' The control will allow no more than 14 characters.
   TextBox1.MaxLength = 14
End Sub

// C#
private void InitializeMyControl()
{
   // Set to no text.
   textBox1.Text = "";
   // The password character is an asterisk.
   textBox1.PasswordChar = '*';
   // The control will allow no more than 14 characters.
   textBox1.MaxLength = 14;
}

// C++
private:
   void InitializeMyControl()
   {
      // Set to no text.
      textBox1->Text = "";
      // The password character is an asterisk.
      textBox1->PasswordChar = '*';
      // The control will allow no more than 14 characters.
      textBox1->MaxLength = 14;
   }

See Also

Introduction to the Windows Forms TextBox Control | Controlling the Insertion Point in a Windows Forms TextBox Control | Creating a Read-Only Text Box (Windows Forms) | Putting Quotation Marks in a String Programmatically (Windows Forms) | Selecting Text Programmatically in the Windows Forms TextBox Control | Viewing Multiple Lines in the Windows Forms TextBox Control | TextBox Class | TextBox Control (Windows Forms)

Show:
© 2016 Microsoft