2013-07-23 10 views
7

Ho un textbox in un modulo di windows C# sto riscontrando problemi nell'assegnazione di valori nulli a PasswordChar. Quello che voglio fare è che se un checkbox è spuntato allora il PasswordChar dovrebbe essere null i.e il testo attuale dovrebbe essere visualizzato altrimenti il ​​PasswordChar dovrebbe essere *. Questo quello che ho provatoNull Password Char in Winform

private void checkBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     if (!checkBox1.Checked) 
     { 
      txtPassword.PasswordChar = '*'; 
     } 
     else 
     { 
      txtPassword.PasswordChar = ''; 
     } 
    } 

ma questa linea

 txtPassword.PasswordChar = ''; 

si genera un errore. Ho anche provato

 txtPassword.PasswordChar = null; 

ma ho ancora un errore.

Si prega di aiutarmi a correggere il mio codice.

risposta

16

Per ripristinare PassswordChar, fare questo txtPassword.PasswordChar = '\0';

Per la vostra comodità:

private void checkBox1_CheckedChanged(object sender, EventArgs e){ 
    txtPassword.PasswordChar = checkBox1.Checked ? '*' : '\0'; 
} 
+0

Grazie perfettamente funziona –

0

Hai provato leggendo il manuale per TextBox.PasswordChar?

Impostare il valore di questa proprietà su 0 (valore carattere) se non si desidera che il controllo copi i caratteri mentre vengono digitati.

1

utilizzando questo codice per impostare carattere password nulla

textBox1.PasswordChar = (char)0; 

o questo

textBox1.PasswordChar = '\0'; 
1

Per Ulteriore Informazioni:

C'è un'alternativa in TextBox.PasswordChar, è anche possibile utilizzare TextBox.UseSystemPasswordChar .

private void checkBox1_CheckedChanged(object sender, EventArgs e){ 
    textBox1.UseSystemPasswordChar = checkBox1.Checked ? true : false; 
} 
+0

funziona perfettamente, grazie –