Export (0) Print
Expand All

TextBox.PasswordChar Property

Gets or sets the character used to mask characters of a password in a single-line TextBox control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public char PasswordChar { get; set; }

Property Value

Type: System.Char
The character used to mask characters entered in a single-line TextBox control. Set the value of this property to 0 (character value) if you do not want the control to mask characters as they are typed. Equals 0 (character value) by default.

The UseSystemPasswordChar property has precedence over the PasswordChar property. Whenever the UseSystemPasswordChar is set to true, the default system password character is used and any character set by PasswordChar is ignored.

When the PasswordChar property is set, cut, copy, and paste actions in the control using the keyboard cannot be performed.

Important noteImportant Note:

When the TextBox is in password mode because PasswordChar, UseSystemPasswordChar, or ReadOnly is true, the TextBox is in restricted mode. In this mode, the ImeMode is disabled, but the current ImeMode is cached so that it can be restored if the TextBox ever becomes unrestricted. Toggling the ReadOnly is a common scenario. The ImeMode is shadowed while the control is in restricted mode. From the designer perspective, the ImeMode value shown is the actual value.

The following code example creates a TextBox control that is used to accept a password. This example uses the CharacterCasing property to change all characters typed to lowercase and the MaxLength property to restrict the password length to eight characters. This example also uses the TextAlign property to center the password in the TextBox control.

public void CreateMyPasswordTextBox()
 {
    // Create an instance of the TextBox control.
    TextBox textBox1 = new TextBox();
    // Set the maximum length of text in the control to eight.
    textBox1.MaxLength = 8;
    // Assign the asterisk to be the password character.
    textBox1.PasswordChar = '*';
    // Change all text entered to be lowercase.
    textBox1.CharacterCasing = CharacterCasing.Lower;
    // Align the text in the center of the TextBox control.
    textBox1.TextAlign = HorizontalAlignment.Center;
 }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft