2011-11-18 10 views
8

come può la password textbox che impostato su:come posso smascherare C# password di testo e la maschera di nuovo alla parola

password_txtBox.PasswordChar ="*" 

da smascherare (da casella di controllo) e quindi maschera di nuovo
senza perdere la stringa all'interno della textbox

+5

WinForms, WPF, o ASP.NET? – David

+1

Metro? WinForms? WPF? Silverlight? ASP.Net? MonoTouch? – SLaks

+0

Si prega di leggere almeno la documentazione prima di fare domande del genere. come sottolineato da @Renaud, si trova nel primo paragrafo della documentazione MSDN. –

risposta

18

Per WinForms:

private void checkBoxShowPassword_CheckedChanged(object sender, EventArgs e) { 
    textBoxPassword.PasswordChar = checkBoxShowPassword.Checked ? '\0' : '*'; 
} 
+0

"PasswordChar" viene utilizzato nell'applicazione winform come din in asp.net con C# –

+0

Per abbinare il carattere utilizzato per mascherare le password in Windows, usare '●' o uno di questi se si ha voglia di fantasia: '○' '◌' '●' '◯' '❍' '✪' – c00000fd

2

se si sta lavorando con interruttore a levetta poi

private void toggleSwitch1_Toggled(object sender, EventArgs e) 
    { 


     if (toggleSwitch1.IsOn==true) 
     { 
      string a= textBox2.Text; 
      textBox2.PasswordChar = '\0'; 
     } 
     if (toggleSwitch1.IsOn==false) 
     { 
      textBox2.PasswordChar = '*'; 
     } 
    } 

qui '\ 0' visualizzerà una password archiviato in testo semplice

+0

Test di migliori condizioni che abbia mai visto: o –

1

txtPassword è la casella di testo password, chkSeePassword è la casella di controllo Mostra password. Ora aggiungere un po 'di codice all'evento CheckedChanged della casella

private void chkSeePassword_CheckedChanged(object sender, EventArgs e) 
{ 
     txtPassword.UseSystemPasswordChar = !chkSeePassword.Checked; 
} 
0

uso questo

private void checkBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     textBox2.PasswordChar = default(char); 
    } 
0

La versione VB.Net è

Private Sub checkBoxShowPassword_CheckedChanged(sender As Object, e As System.EventArgs) Handles checkBoxShowPassword.CheckedChanged 
    textBoxPassword.PasswordChar = If(checkBoxShowPassword.Checked, ControlChars.NullChar, "*"C) 
End Sub 

o

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged 
    If CheckBox1.Checked Then 
     Me.txt_password.PasswordChar = "*"c 
    Else 
     Me.txt_password.PasswordChar = ControlChars.NullChar 
    End If 
End Sub 
Problemi correlati