C#-水印passwordchar?

C# - Watermark passwordchar?
2021-05-11
  •  译文(汉语)
  •  原文(英语)

我目前正在为我的程序制作一个登录表单,其中有两个文本框"电子邮件"和"密码"的水印.

当文本框为空时,其水印文本将显示在其中,如"在此处输入电子邮件"和"在此处输入密码".

到目前为止,我的代码是:

    private void emailLogin_Leave(object sender, EventArgs e){
        if (emailLogin.Text.Length == 0){
            emailLogin.Text = "Email";
            emailLogin.ForeColor = Color.Silver;
        }
    }

    private void emailLogin_Enter(object sender, EventArgs e){
        if (emailLogin.Text == "Email"){
            emailLogin.Text = "";
            emailLogin.ForeColor = Color.Black;
        }
    }

    private void passwordLogin_Leave(object sender, EventArgs e){
        if (passwordLogin.Text.Length == 0){
            passwordLogin.Text = "Password";
            passwordLogin.ForeColor = Color.Silver;
        }
    }

    private void passwordLogin_Enter(object sender, EventArgs e){
        if (passwordLogin.Text == "Password"){
            passwordLogin.Text = "";
            passwordLogin.ForeColor = Color.Black;
        }
    }

但是现在我的问题是我想使用密码字符作为密码.但是我仍然希望水印文本为常规文本.当我检查使用密码字符时,它将我的水印变成"* *",而不是"Password".我该如何解决?

顺便说一句,我不想​​使用"UseSystemPasswordChar"(那些点).我想使用"PasswordChar"并使用星号(*)作为密码字符.

速聊1:
您能否添加标签以说明此问题与Windows Forms或WPF有关.
速聊2:
在WPF中,文本框具有Foreground属性而不是ForeColor.所以是Windows窗体
解决过程1

只需像设置ForeColor一样设置并取消设置即可:

    private void passwordLogin_Leave(object sender, EventArgs e){
        if (passwordLogin.Text.Length == 0){
            passwordLogin.Text = "Password";
            passwordLogin.ForeColor = Color.Silver;
            passwordLogin.PasswordChar = '\0';
        }
    }

    private void passwordLogin_Enter(object sender, EventArgs e){
        if (passwordLogin.Text == "Password"){
            passwordLogin.Text = "";
            passwordLogin.ForeColor = Color.Black;
            passwordLogin.PasswordChar = '*';
        }
    }
速聊1:
很棒!太感谢了!我不知道\ 0.这到底是什么意思?再次,谢谢你!:)
速聊2:
\0 只是空字符,即为空.
速聊3:
就是这么简单,哈哈!我试图解决这个问题长达45分钟.谢谢!:)
速聊4:
\ 0并不是真正的"无"-仅仅是PasswordChar属性将零视为具有"不显示密码掩码"的特殊含义

I'm currently making a login form to my program where I have a watermark for the two textboxes Email and Password.

When a textbox is empty, its watermark text will appear in it like "Enter email here" and "Enter password here".

My code so far is:

    private void emailLogin_Leave(object sender, EventArgs e){
        if (emailLogin.Text.Length == 0){
            emailLogin.Text = "Email";
            emailLogin.ForeColor = Color.Silver;
        }
    }

    private void emailLogin_Enter(object sender, EventArgs e){
        if (emailLogin.Text == "Email"){
            emailLogin.Text = "";
            emailLogin.ForeColor = Color.Black;
        }
    }

    private void passwordLogin_Leave(object sender, EventArgs e){
        if (passwordLogin.Text.Length == 0){
            passwordLogin.Text = "Password";
            passwordLogin.ForeColor = Color.Silver;
        }
    }

    private void passwordLogin_Enter(object sender, EventArgs e){
        if (passwordLogin.Text == "Password"){
            passwordLogin.Text = "";
            passwordLogin.ForeColor = Color.Black;
        }
    }

But now my problem is that I want to use password characters for the password. But I still want the watermark text to be in regular text. When I check to use a password char it turns my watermark into "**" instead of "Password". How can I fix that?

By the way, I don't want to use the "UseSystemPasswordChar" (those dots). I want to use "PasswordChar" and use asterisks (*) as password chars.

Talk1:
Could you add a tag to state whether this question is Windows Forms or WPF related please.
Talk2:
In WPF, Textbox has Foreground property not ForeColor. So it's Windows Forms
Solutions1

Just set it and unset it as you do for the ForeColor :

    private void passwordLogin_Leave(object sender, EventArgs e){
        if (passwordLogin.Text.Length == 0){
            passwordLogin.Text = "Password";
            passwordLogin.ForeColor = Color.Silver;
            passwordLogin.PasswordChar = '\0';
        }
    }

    private void passwordLogin_Enter(object sender, EventArgs e){
        if (passwordLogin.Text == "Password"){
            passwordLogin.Text = "";
            passwordLogin.ForeColor = Color.Black;
            passwordLogin.PasswordChar = '*';
        }
    }
Talk1:
Works great! Thank you so much! I did not know about the \0. What exactly does it mean? Again, thank you so much! :)
Talk2:
\0 is just the null character, i.e. nothing.
Talk3:
So it was that easy, haha! I was trying to figure this out for a good 45 minutes. Thanks! :)
Talk4:
\0 is not really 'nothing' - it's just that the PasswordChar property treats zero as having the special meaning of 'don't display password masking'
转载于:https://stackoverflow.com/questions/20217557/c-sharp-watermark-passwordchar

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入