C# 如何取消密码文本框的屏蔽并将其屏蔽回密码?

C# 如何取消密码文本框的屏蔽并将其屏蔽回密码?,c#,textbox,C#,Textbox,如何将密码文本框设置为: password_txtBox.PasswordChar ="*" 取消屏蔽(从复选框),然后再次屏蔽 在不松开winforms文本框内的字符串的情况下: 只需将属性设置为“\0”(这是默认值)即可不屏蔽字符 资料来源: 注意:请注意“\0”与“0”不同。第一个是空字符,白色“0”是将显示为0的字符。如果使用切换开关,则 private void toggleSwitch1_Toggled(object sender, EventArgs e) { if (t

如何将密码文本框设置为:

password_txtBox.PasswordChar ="*"
取消屏蔽(从复选框),然后再次屏蔽
在不松开winforms文本框内的字符串的情况下:


只需将属性设置为“\0”(这是默认值)即可不屏蔽字符

资料来源:


注意:请注意“\0”与“0”不同。第一个是空字符,白色“0”是将显示为0的字符。

如果使用切换开关,则

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

此处
'\0'
将显示密码字段为纯文本txtPassword是密码文本框,chkSeePassword是显示密码复选框。现在向复选框的CheckedChanged事件添加一些代码

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

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        textBox2.PasswordChar = default(char);
    }
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


显示和隐藏密码最简单的方法之一是在密码文本框中使用单选按钮

单选按钮的属性应类似于:

this.radioBtn_ShowHidePassword.AutoCheck = false;    
然后,必须手动处理单击活动,使其与“单击”事件中的当前状态相反

最后是显示和隐藏密码的最简单方法

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
}

我这样做是为了简单地显示密码 (我对编码相当陌生,因此如果这是一个糟糕的做法,欢迎反馈)


Winforms、WPF或ASP.NET?Metro?WinForms?WPF?银灯?ASP.Net?MonoTouch?在您提出类似问题之前,请至少阅读文档。正如@Renaud所指出的,它在MSDN文档的第一段。@EricLiprandi:就我个人而言,我认为问这个问题没有任何问题。也许它就在你面前,因为你知道去哪里看?查找您要查找的信息的能力需要经验,而不是每个人都处于同一级别。winform应用程序中使用了相关帖子-、、&“PasswordChar”如何在asp.net中使用C#要匹配用于在Windows中屏蔽密码的字符,请使用
或其中任何一种,如果您喜欢:
‬ <代码>●
文档有点不具体。澄清“字符值”是指“\0”,而不是“0”,可能会有所帮助。因此:password_txtBox.PasswordChar='\0'。否则,您将得到零作为掩蔽字符。我不需要wokring:(
this.radioBtn_ShowHidePassword.AutoCheck = false;    
private void radioBtn_ShowHidePassword_Click(object sender, EventArgs e)
{
 radioBtn_ShowHidePassword.Checked = (! radioBtn_ShowHidePassword.Checked);
}
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
}
 private void buttonShowPassword_MouseDown(object sender, MouseEventArgs e)
    {
        TextBoxPassword.Properties.PasswordChar =(char)0;
    }

    private void buttonShowPassword_MouseUp(object sender, MouseEventArgs e)
    {
        TextBoxPassword.Properties.PasswordChar = '*';
    }