C# Winform 密码框 在textBox中按下一个健就显示该健 放掉就显示* 如**8 输入下一个的时候上一个就显示* 在线等 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 类似这样,自己完善吧 private void textBox3_KeyDown(object sender, KeyEventArgs e) { if (textBox3.Tag == null) { textBox3.Tag = e.KeyCode.ToString(); } else { string str = ""; str = str.PadLeft(textBox3.Tag.ToString().Length, '*') + e.KeyCode.ToString(); textBox3.Text = str; textBox3.Tag = textBox3.Tag.ToString() + e.KeyCode.ToString(); } } 就是文本框事件里,将用户输入的字符用*号replace掉,自己声明一个字符串来保存用户的实际输入 定义一个string变量用来存真正的密码,然后在textbox事件里,将密码前n-1位replace成"*",只显示最后一位。当textbox失去焦点的时候,所有的密码都显示"*"。 public class MyTextBox : TextBox { private Timer _Timer; public MyTextBox() { this.KeyUp += TextBoxKeyUp; _Timer = new Timer(); _Timer.Tick += Tick; _Timer.Interval = 200; } private void TextBoxKeyUp(object sender, EventArgs e) { _Timer.Start(); } private void Tick(object sender, EventArgs e) { _Timer.Stop(); Text = new String('*', Text.Length); this.SelectionStart = Text.Length; } }原来的值怎么保存呢? 很好实现的,在textChaned事件中先将无效值都过滤掉,输入的时候只保留最后一位,其它都替换成"*",密码没多长,这个效率可以忽略,当文本框没有焦点时,全部都替换成"*" 我说简单是我曾经实现过类似的,我说了先要过滤无效字符和操作,只有当光标在最后一个字符,backspace才可用,就是说只能删除最后一个字符,其它操作包括复制,粘贴,剪切,从中间插入等都是无效的 StringBuilder sb = new StringBuilder(); private void textBox1_KeyUp(object sender, KeyEventArgs e) { if (textBox1.Text.Trim().Length > 0) { string s=textBox1.Text.Substring(textBox1.Text.Length-1,1); sb.Append(s); textBox1.Text=textBox1.Text.Trim().Replace(s,"*"); textBox1.SelectionStart = textBox1.Text.Length; } } 可以用文本框的key_up,与key_down两个事件配合着来完成。也可以用字符串的函数操作来实现。具体做法会有很多。具体做法,自己解决了,才会体会到解决问题的兴奋。 随便写的一个,轻点喷撒public partial class Form1 : Form { public Form1() { InitializeComponent(); textBox1.ContextMenu = new ContextMenu(); textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (flag == 1) { flag = 0; return; } flag++; if (textBox1.SelectionStart == textBox1.Text.Length) { cando = true; } if (textBox1.SelectionStart < textBox1.Text.Length || (int)e.KeyChar == 22 || (int)e.KeyChar >= 112 && e.KeyChar == 115 || e.KeyChar == 24 || e.KeyChar == 3 && e.KeyChar == 8 && e.KeyChar == 13) { e.Handled = true; cando = false; } if ((int)e.KeyChar == 8 && textBox1.Text.Length > 0) { int sta = sb.ToString().Length - 1; if (sta >= 0) sb.Remove(sta, 1); isback = true; } else { isback = false; } } bool cando = true; int flag = 0; bool isback = false; StringBuilder sb = new StringBuilder(); private void textBox1_KeyUp(object sender, KeyEventArgs e) { if (!cando) { return; } if (textBox1.Text.Trim().Length > 0 && !isback) { isback = false; string s = textBox1.Text.Substring(textBox1.Text.Length - 1, 1); sb.Append(s); textBox1.Text = textBox1.Text.Trim().Replace(s, "*"); textBox1.SelectionStart = textBox1.Text.Length; MessageBox.Show(sb.ToString()); } else { MessageBox.Show(sb.ToString()); } } } 我去,csdn不及时更新,害我发了2次回复, 尼玛。都怎么回答的。控件有个属性textBox1.PasswordChar = '*'; 看错了。对不住大家textBox1.PasswordChar = '\0';可以还原可以计时开启属性或离开焦点 21楼的s不能输以及只处理了只能一次输对密码 不能修改我对其中进行了部分修改里面还是有蛮多bug的因为找到其他方法了 就没处理了 先把这代码贴下 /// <summary> /// 用于记录密码 /// </summary> private StringBuilder StrPassword = new StringBuilder(); private void password_KeyDown(object sender, KeyEventArgs e) { if (password.Text.Length > 0) { if (e.KeyValue == 46) { if (password.SelectionStart >= password.Text.Length) { return; } else { isDel = true; if (password.SelectionStart != 0) { mouseStart = password.SelectionStart; } else { mouseStart = 0; } int leg = password.SelectionLength == 0 ? 1 : password.SelectionLength; try { password.Text = password.Text.Remove(mouseStart, leg - 1); StrPassword.Remove(mouseStart, leg); } catch { } password.SelectionStart = mouseStart; } } else { if ((int)e.KeyValue == 22 || e.KeyValue == 24 || e.KeyValue == 3 && e.KeyValue == 8 && e.KeyValue == 13) { } else if (password.SelectionLength > 0) { isResetPWD = true; resetPWD = password.SelectionLength; mouseStart = password.SelectionStart; StrPassword.Remove(mouseStart, resetPWD); } isDel = false; } } } bool isResetPWD = false;//选择部分密码时输入密码 bool isDel = false; int mouseStart = 0; int resetPWD = 0; //选中部分密码时选中的长度 bool cando = true; bool isback = false; //是否按的是后退 private void password_KeyPress(object sender, KeyPressEventArgs e) { if (password.SelectionStart == password.Text.Length) { cando = true; } if ((int)e.KeyChar == 22 || e.KeyChar == 24 || e.KeyChar == 3 && e.KeyChar == 8 && e.KeyChar == 13) { e.Handled = true; cando = false; } if (e.KeyChar == 8) { if (password.SelectionStart == 0 && password.SelectionLength == 0) { return; } else { isback = true; mouseStart = password.SelectionStart; try { if (password.SelectionLength == 0) { password.Text = password.Text.Remove(mouseStart - 1, 1); StrPassword.Remove(mouseStart - 1, 1); password.SelectionStart = mouseStart - 1; } else { password.Text = password.Text.Remove(mouseStart, password.SelectionLength); StrPassword.Remove(mouseStart, password.SelectionLength); password.SelectionStart = mouseStart; } } catch { } } } else { isback = false; } } private void password_KeyUp(object sender, KeyEventArgs e) { if (password.Text == "") { StrPassword.Length = 0; } } private void password_TextChanged(object sender, EventArgs e) { if (!cando) { return; } if (password.Text.Trim().Length > 0 && !isback && !isDel) { isback = false; isDel = false; if (isResetPWD) { string ss = password.Text.Substring(mouseStart, 1); if (ss != "*") { string endstr = StrPassword.ToString().Insert(mouseStart, ss); StrPassword = new StringBuilder(endstr); } if (password.Text.Length > 1) { string sstart = password.Text.Substring(0, 1); if (sstart != "*") { StrPassword.Append(ss); password.Text = password.Text.Trim().Replace(sstart, "*"); } } isResetPWD = false; } string s = password.Text.Substring(password.Text.Length - 1, 1); if (s != "*") StrPassword.Append(s); if (password.Text.Length > 1) { string sstart = password.Text.Substring(0, 1); if (sstart != "*") { StrPassword.Append(s); password.Text = password.Text.Trim().Replace(sstart, "*"); } } password.Text = password.Text.Trim().Replace(s, "*"); password.SelectionStart = password.Text.Length; } } 能不能让VS.net编译完项目,自动对代码进行混淆? IEnumerable.Where的奇怪问题 多线程问题. asp.net保存文件对话框 请问如何将实体数据在泛型中引用?? 这样调用MSXML2.XMLHTTP为什么会出错 Substring的一个问题 有关DevExpress问题相关,求助 怎样取得arraylist里的值 显示光标问题 同步不同数据库中的表 【求助】C#写的程序有什么办法能在安桌系统的手机上运行
{
if (textBox3.Tag == null)
{
textBox3.Tag = e.KeyCode.ToString();
}
else
{
string str = "";
str = str.PadLeft(textBox3.Tag.ToString().Length, '*') + e.KeyCode.ToString();
textBox3.Text = str;
textBox3.Tag = textBox3.Tag.ToString() + e.KeyCode.ToString();
}
}
{
private Timer _Timer; public MyTextBox()
{
this.KeyUp += TextBoxKeyUp; _Timer = new Timer();
_Timer.Tick += Tick;
_Timer.Interval = 200;
} private void TextBoxKeyUp(object sender, EventArgs e)
{
_Timer.Start();
} private void Tick(object sender, EventArgs e)
{
_Timer.Stop();
Text = new String('*', Text.Length);
this.SelectionStart = Text.Length;
}
}原来的值怎么保存呢?
在textChaned事件中先将无效值都过滤掉,
输入的时候只保留最后一位,其它都替换成"*",
密码没多长,这个效率可以忽略,
当文本框没有焦点时,全部都替换成"*"
我说了先要过滤无效字符和操作,
只有当光标在最后一个字符,backspace才可用,
就是说只能删除最后一个字符,
其它操作包括复制,粘贴,剪切,从中间插入等都是无效的
StringBuilder sb = new StringBuilder();
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (textBox1.Text.Trim().Length > 0)
{
string s=textBox1.Text.Substring(textBox1.Text.Length-1,1);
sb.Append(s);
textBox1.Text=textBox1.Text.Trim().Replace(s,"*");
textBox1.SelectionStart = textBox1.Text.Length;
}
}
随便写的一个,轻点喷撒public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.ContextMenu = new ContextMenu(); textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (flag == 1)
{
flag = 0;
return;
}
flag++; if (textBox1.SelectionStart == textBox1.Text.Length)
{
cando = true;
} if (textBox1.SelectionStart < textBox1.Text.Length || (int)e.KeyChar == 22 || (int)e.KeyChar >= 112 && e.KeyChar == 115 || e.KeyChar == 24 || e.KeyChar == 3 && e.KeyChar == 8 && e.KeyChar == 13)
{
e.Handled = true;
cando = false;
} if ((int)e.KeyChar == 8 && textBox1.Text.Length > 0)
{ int sta = sb.ToString().Length - 1;
if (sta >= 0)
sb.Remove(sta, 1);
isback = true;
}
else
{
isback = false;
} }
bool cando = true;
int flag = 0;
bool isback = false;
StringBuilder sb = new StringBuilder();
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!cando)
{
return;
} if (textBox1.Text.Trim().Length > 0 && !isback)
{ isback = false;
string s = textBox1.Text.Substring(textBox1.Text.Length - 1, 1);
sb.Append(s); textBox1.Text = textBox1.Text.Trim().Replace(s, "*");
textBox1.SelectionStart = textBox1.Text.Length;
MessageBox.Show(sb.ToString());
}
else
{
MessageBox.Show(sb.ToString());
}
}
}
可以还原
可以计时开启属性
或离开焦点
以及只处理了只能一次输对密码 不能修改
我对其中进行了部分修改
里面还是有蛮多bug的因为找到其他方法了 就没处理了 先把这代码贴下
/// <summary>
/// 用于记录密码
/// </summary>
private StringBuilder StrPassword = new StringBuilder();
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (password.Text.Length > 0)
{
if (e.KeyValue == 46)
{
if (password.SelectionStart >= password.Text.Length)
{
return;
}
else
{
isDel = true;
if (password.SelectionStart != 0)
{
mouseStart = password.SelectionStart;
}
else
{
mouseStart = 0;
}
int leg = password.SelectionLength == 0 ? 1 : password.SelectionLength;
try
{
password.Text = password.Text.Remove(mouseStart, leg - 1);
StrPassword.Remove(mouseStart, leg);
}
catch
{
}
password.SelectionStart = mouseStart;
}
}
else
{
if ((int)e.KeyValue == 22 || e.KeyValue == 24 || e.KeyValue == 3 && e.KeyValue == 8 && e.KeyValue == 13)
{ }
else if (password.SelectionLength > 0)
{
isResetPWD = true;
resetPWD = password.SelectionLength;
mouseStart = password.SelectionStart;
StrPassword.Remove(mouseStart, resetPWD);
}
isDel = false;
}
}
}
bool isResetPWD = false;//选择部分密码时输入密码
bool isDel = false;
int mouseStart = 0;
int resetPWD = 0; //选中部分密码时选中的长度
bool cando = true;
bool isback = false; //是否按的是后退
private void password_KeyPress(object sender, KeyPressEventArgs e)
{
if (password.SelectionStart == password.Text.Length)
{
cando = true;
} if ((int)e.KeyChar == 22 || e.KeyChar == 24 || e.KeyChar == 3 && e.KeyChar == 8 && e.KeyChar == 13)
{
e.Handled = true;
cando = false;
}
if (e.KeyChar == 8)
{
if (password.SelectionStart == 0 && password.SelectionLength == 0)
{
return;
}
else
{
isback = true;
mouseStart = password.SelectionStart;
try
{
if (password.SelectionLength == 0)
{
password.Text = password.Text.Remove(mouseStart - 1, 1);
StrPassword.Remove(mouseStart - 1, 1);
password.SelectionStart = mouseStart - 1;
}
else
{
password.Text = password.Text.Remove(mouseStart, password.SelectionLength);
StrPassword.Remove(mouseStart, password.SelectionLength);
password.SelectionStart = mouseStart;
}
}
catch
{
} }
}
else
{ isback = false;
}
} private void password_KeyUp(object sender, KeyEventArgs e)
{
if (password.Text == "")
{
StrPassword.Length = 0;
}
} private void password_TextChanged(object sender, EventArgs e)
{
if (!cando)
{
return;
}
if (password.Text.Trim().Length > 0 && !isback && !isDel)
{
isback = false;
isDel = false;
if (isResetPWD)
{
string ss = password.Text.Substring(mouseStart, 1);
if (ss != "*")
{
string endstr = StrPassword.ToString().Insert(mouseStart, ss);
StrPassword = new StringBuilder(endstr);
}
if (password.Text.Length > 1)
{
string sstart = password.Text.Substring(0, 1);
if (sstart != "*")
{
StrPassword.Append(ss);
password.Text = password.Text.Trim().Replace(sstart, "*");
}
}
isResetPWD = false;
}
string s = password.Text.Substring(password.Text.Length - 1, 1);
if (s != "*")
StrPassword.Append(s);
if (password.Text.Length > 1)
{
string sstart = password.Text.Substring(0, 1);
if (sstart != "*")
{
StrPassword.Append(s);
password.Text = password.Text.Trim().Replace(sstart, "*");
}
}
password.Text = password.Text.Trim().Replace(s, "*"); password.SelectionStart = password.Text.Length;
}
}