textbox:密码字符(将单词的字母一个一个改成星号) [英] textbox: password character (changing the letter of the word into asterisk one by one)
问题描述
我的登录表单中的密码文本框有问题.我希望它比其他人更精确,这就是我想知道的:
I have a problem on my password text box in log-in form. I want it to be more precise than the others, this is what i want to know:
我会在文本框中输入password"这个词,如果我输入第一个字母p"我看到它是字母p,然后如果我输入第二个字母a",字母p"" 现在会变成 "*" 而第二个字母也可以被看作是 "a",然后一直到 "password" 变成 "*******d".然后如果我离开那个文本框,剩下的字母也会变成*".我试图去属性并寻找密码字符,但它不符合我的需要.我该怎么做?
I will write the word "password" in the text box, if i type the first letter "p" i see that it is letter p, then if i type the second letter which is "a", the letter "p" will now become "*" while the second letter can also be seen as "a", and then it goes until the word "password" become "*******d". And then if I leave that text box the remaining letter will become "*" also. I tried to go to the properties and look for password character, but it doesn't meet my needs. How can i do that ?
推荐答案
这里我根据 Textbox 控件的 KEY_UP 和 LEAVE 事件为您创建一个简单的示例.
Here i create a simple example for you base on KEY_UP and LEAVE events of Textbox control.
使用此代码,您需要进行一些额外的工作才能使其完美,例如:
With this code you need some additional works to take it prefect like:
在 KEY_UP 上忽略不是字母数字和特殊字符的键,例如(退格、回车、Shift、回车和...)
Ignore Keys that are not Alphanumeric and special characters like (Back-space, Enter, Shift, Enter and...) On KEY_UP
测试并享受.... ;)
private void txtxPassword_KeyUp(object sender, KeyEventArgs e)
{
var pass = txtxPassword.Text;
if (pass.Length < 1)
realPass = string.Empty;
else
realPass += pass[pass.Length - 1];
if (e.KeyCode == Keys.Back)
{
if (realPass.Length <= 1)
realPass = "";
else
{
realPass = realPass.Substring(0, realPass.Length - 2);
}
}
string result = "";
if (realPass != string.Empty)
{
for (var i = 0; i < realPass.Length - 1; i++)
{
result += "X";
}
result += realPass[realPass.Length - 1];
}
txtxPassword.Text = result;
txtxPassword.SelectionStart = result.Length;
lblpass.Text = realPass;
}
private void txtxPassword_Leave(object sender, EventArgs e)
{
string result="";
for (var i = 0; i < realPass.Length ; i++)
{
result += "X";
}
txtxPassword.Text = result;
lblpass.Text = realPass;
}
结果:
....................................................................................................
..........................................................................................
编辑
您必须将 RealPassword 保存在变量中.当您更改 textbox.text 时,您的 RealPassword 将被新字符丢失.所以我清除了一个名为 realPass 的变量来保留实际插入的密码.它必须是您班级中的通用变量,如下所示:
You must keep the RealPassword in a variable. When you change the textbox.text, your RealPassword will be lost by new characters. So I declear a variable named realPass to keep the actual inserted password. It must be a general variabel in your class like this:
private string realPass="";
这篇关于textbox:密码字符(将单词的字母一个一个改成星号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!