TextBox.PasswordChar 属性

定义

获取或设置字符,该字符用于屏蔽单行 TextBox 控件中的密码字符。Gets or sets the character used to mask characters of a password in a single-line TextBox control.

public:
 property char PasswordChar { char get(); void set(char value); };
public char PasswordChar { get; set; }
member this.PasswordChar : char with get, set
Public Property PasswordChar As Char

属性值

用于屏蔽在单行 TextBox 控件中输入的字符的字符。The character used to mask characters entered in a single-line TextBox control. 如果你不希望控件在键入时屏蔽字符,请将此属性的值设置为 "0" (U + 0000)。Set the value of this property to '0' (U+0000) if you do not want the control to mask characters as they are typed. 默认值为 "0" (U + 0000)。The default value is '0' (U+0000).

示例

下面的代码示例创建一个用于接受密码的 TextBox 控件。The following code example creates a TextBox control that is used to accept a password. 此示例使用 CharacterCasing 属性将键入的所有字符更改为小写,并使用 MaxLength 属性将密码长度限制为8个字符。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. 此示例还使用 TextAlign 属性使密码在 TextBox 控件中居中。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 = gcnew 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;
   }
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;
 }
 
Public Sub CreateMyPasswordTextBox()
    ' Create an instance of the TextBox control.
    Dim textBox1 As 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 = "*"c
    ' 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
End Sub

注解

UseSystemPasswordChar 属性优先于 PasswordChar 属性。The UseSystemPasswordChar property has precedence over the PasswordChar property. 每当将 UseSystemPasswordChar 设置为 true时,将使用默认系统密码字符并忽略 PasswordChar 的任何字符集。Whenever the UseSystemPasswordChar is set to true, the default system password character is used and any character set by PasswordChar is ignored.

如果设置了 PasswordChar 属性,则无法执行使用键盘的控件中的 "剪切" 和 "复制" 操作。When the PasswordChar property is set, cut and copy actions in the control using the keyboard cannot be performed.

重要

如果 TextBox 处于密码模式,因为 PasswordCharUseSystemPasswordCharReadOnly true,则 TextBox 处于受限模式下。When the TextBox is in password mode because PasswordChar, UseSystemPasswordChar, or ReadOnly is true, the TextBox is in restricted mode. 在此模式下,将禁用 ImeMode,但会缓存当前 ImeMode,以便在 TextBox 变得不受限制时可以还原。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. 切换 ReadOnly 是一种常见方案。Toggling the ReadOnly is a common scenario. 当控件处于受限模式时,ImeMode 为隐藏状态。The ImeMode is shadowed while the control is in restricted mode. 从设计器的角度来看,显示的 ImeMode 值是实际值。From the designer perspective, the ImeMode value shown is the actual value.

适用于