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

屬性值

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 密碼長度限制為八個字元的屬性。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 ,就會使用預設系統密碼字元,並忽略所設定的任何字元 PasswordCharWhenever 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.

適用於