How can I unmask password text box and mask it back to password?

前端 未结 7 1056
情深已故
情深已故 2020-11-27 21:36

How can password textbox that set to :

password_txtBox.PasswordChar =\"*\"

to be unmasked ( from checkbox ) and then mask again
without

相关标签:
7条回答
  • 2020-11-27 21:58

    One of the easiest method to show and hide password is by using radio button inside password text box

    The properties of radio button should be like:

    this.radioBtn_ShowHidePassword.AutoCheck = false;    
    

    then the clicking activity has to be taken care manually just making it to be reverse of present state in its "Click" event

    private void radioBtn_ShowHidePassword_Click(object sender, EventArgs e)
    {
     radioBtn_ShowHidePassword.Checked = (! radioBtn_ShowHidePassword.Checked);
    }
    

    then finally the easiest way of show and hide password

    private void radioBtn_ShowHidePassword_CheckedChanged(object sender, EventArgs e)
    {
       txtBoxPassword.PasswordChar = radioBtn_ShowHidePassword.Checked ? '\0' : '*';
       // here we can even include the code for changing the default picture of button to two different
       //representation like closed eye and opened eye which resembles Windows login
    }
    
    0 讨论(0)
  • 2020-11-27 22:00

    If you are working with toggle switch then

    private void toggleSwitch1_Toggled(object sender, EventArgs e)
    {
        if (toggleSwitch1.IsOn)
        {
            string a= textBox2.Text;
            textBox2.PasswordChar = '\0';
        }
        else
        {
            textBox2.PasswordChar = '*';
        }
    }
    

    here '\0' will show to password filed to plain text

    0 讨论(0)
  • 2020-11-27 22:01

    For winforms:

    private void checkBoxShowPassword_CheckedChanged(object sender, EventArgs e) {
       textBoxPassword.PasswordChar = checkBoxShowPassword.Checked ? '\0' : '*';
    }
    
    0 讨论(0)
  • 2020-11-27 22:11

    The VB.Net version is

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

    or

    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
    
    0 讨论(0)
  • 2020-11-27 22:16

    Just set the property to '\0' (which is the default value) to not mask characters.

    Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.passwordchar.aspx

    Note: notice that '\0' is different from '0'. The first one is the null character, white '0' is the character that will be displayed as 0.

    0 讨论(0)
  • 2020-11-27 22:16

    txtPassword is the Password textbox, chkSeePassword is the Show password checkbox. Now add some code to the checkbox's CheckedChanged event

    private void chkSeePassword_CheckedChanged(object sender, EventArgs e)
    {
            txtPassword.UseSystemPasswordChar = !chkSeePassword.Checked;
    }
    
    0 讨论(0)
提交回复
热议问题