TextBox.PasswordChar 屬性

定義

取得或設定在單行 TextBox 控制項中用來遮罩密碼字元的字元。

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 控制項中已輸入字元的字元。 如果您不想讓控制項在輸入字元時遮罩字元,請將此屬性的值設定為 '0' (U+0000) 。 預設值為 '0' (U+0000) 。

範例

下列程式碼範例會 TextBox 建立用來接受密碼的 控制項。 本範例會 CharacterCasing 使用 屬性,將輸入的所有字元變更為小寫,而 MaxLength 屬性則會將密碼長度限制為八個字元。 這個範例也會使用 TextAlign 屬性將 控制項中的密碼置中 TextBox

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 屬性。 每當 UseSystemPasswordChar 設定 true 為 時,就會使用預設系統密碼字元,並忽略 的任何字元集 PasswordChar

PasswordChar設定 屬性時,無法使用鍵盤在 控制項中剪下和複製動作。

重要

TextBox當 處於密碼模式時,因為 PasswordCharUseSystemPasswordCharReadOnlytrue ,則 TextBox 處於受限制模式。 在此模式中 ImeMode ,會停用 ,但會快取目前的 ImeMode ,如此一來 TextBox ,如果 變得不受限制,就可以還原它。 切換 ReadOnly 是常見的案例。 ImeMode當控制項處於受限制模式時,會遮蔽 。 從設計工具的觀點來看, ImeMode 顯示的值是實際值。

適用於