求助,C# 窗体密码框的问题,请教一下坛里的高手们 本人不才,是新手用VS2008最近编一个窗体,密码框我想有这样的功能:比如当用户输入密码6时,密码框会显示6,大概一秒吧然后马上变成"*"星号请问这该怎么做?谢谢了. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在OnChange事件中start一个timer,然后1秒后,将最后一个字符设置为*,但实际的密码内容存储在tag中 直接设PasswordChar属性为*好了 谁输密码时还来得及看看自己输得是什么啊 请问PasswordChar属性是TextBox的吗?还是Form的? PasswordChar是TextBox的属性 你找到这个属性 输入你要显示的符号 如果你输入的是* 那么文本内容都会替换成* 输入其它符号同意替换成你设置的符号 就是TextBox的啊 直接设*号就好了 多方便 呵呵,谢谢你我刚才找半天没找到我猜想起来我密码框用的是RichTextBox难怪会没有 private void textBox1_TextChanged(object sender, EventArgs e) { timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { textBox1.Tag = textBox1.Tag + textBox1.Text.Substring(textBox1.Text.Length - 1, 1); textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + "*"; textBox1.SelectionStart = textBox1.Text.Length; //设置光标到当前输入位置 timer1.Stop(); } timer的interval属性设为了1000你可以根据实际情况加长或减短 这个可以改变textbox的readonly或enable属性来做,不过那样变成灰色有变回来的难看可以这样做 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (textBox1.Text.Length>0 && textBox1.Text.Substring(textBox1.Text.Length - 1, 1) != "*") { e.Handled = true; //如果最后一位不是*,则取消本次(按键)动作 } } 1楼正解先向窗体中加入一个timer控件,再为timer加入Tick事件,并设置间隔时间为1000毫秒,将要实现的效果代码写在Tick事件中就可以了;再为密码控件加入OnChange事件,在这个事件里调用timer的Start()方法即可,注意每执行一次Tick事件后将Timer关了 string m_pwd = ""; Timer timer1 = new Timer(); private void Form1_Load(object sender, EventArgs e) { timer1.Interval = 1000; timer1.Tick += new EventHandler(timer1_Tick); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ss = e.KeyChar; if ((int)e.KeyChar != 8) { textBox1.Text = "".PadLeft(m_pwd.Length, '*') + e.KeyChar; textBox1.SelectionStart = textBox1.Text.Length; m_pwd += e.KeyChar; timer1.Enabled = true; e.Handled = true; } else { m_pwd = ""; textBox1.Text = ""; } } private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = "".PadLeft(m_pwd.Length, '*'); textBox1.SelectionStart = textBox1.Text.Length; timer1.Enabled = false; } 你试试看这段代码,在 TextBox的 TextChanged事件中运行。 private void textBox1_TextChanged(object sender, EventArgs e) { this.textBox1.UseSystemPasswordChar = false; //this.textBox1.Invalidate(); System.Threading.Thread.Sleep(1000); this.textBox1.UseSystemPasswordChar = true; } /// <summary> /// 记录真实的密码 /// </summary> private string realPass = null; private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 8 && realPass != null) { realPass = realPass.Substring(0, realPass.Length - 1); return; } char currentChar = e.KeyChar; realPass += currentChar; //之所以用System.Threading.Timer,是因为这个timer是局部变量的时候 //只执行一次就销毁 System.Threading.Timer tm = new Timer(delegate { this.textBox1.Text = this.textBox1.Text.Replace(currentChar, '*'); }, null, 1, 0); } } 你这样不准确,有可能刚输入一个字符的时候就到了timer的周期了 万分感谢这位大哥经过我的测试之后这位大哥的代码达到我想要的效果就像用户输入的速度很快也不会出现问题前面输入的都为'*'号最后一个输入的显示1秒后才变成'*'号只是代码我有点不怎么理解1,Timer我是使用System.Timers命名空间,为什么大家写的代码中没有使用委托呢?不是说在制定Timer时间到了要触发的方法时就会形成一个隐藏的线程吗?我写的是这个:timers.Elapsed += new ElapsedEventHandler(AntherPasswordDisplay);//AntherPasswordDisplay是我编写的一个方法的方法名,形参列表是(object source, System.Timers.ElapsedEventArgs e)不写这个会报错整个方法为:private void AntherPasswordDisplay(object source, System.Timers.ElapsedEventArgs e){ this.Invoke( (MethodInvoker) delegate { PassswordImputTextBox.Text = "".PadLeft(realPassWordString.Length, '*'); PassswordImputTextBox.SelectionStart = PassswordImputTextBox.Text.Length; }); timers.Stop(); }2,这位大哥在textBox1_KeyPress事件中写的第一句代码:char ss = e.KeyChar; 这句有什么功能,我看全代码中都没有有关变量ss的使用这个ss难道有什么特殊的用途吗? 这位大哥,你写的代码我测试的时候报错了说:错误 1 “System.Windows.Forms.Timer”不包含采用“4”参数的构造函数 F:\Study\编程垃圾\测试\WindowsFormsApplication1\Form1.cs 37 41 WindowsFormsApplication1 char ss = e.KeyChar; 这个删掉 是误打 不好意思 帅锅,我都写了是System.Threading.Timer,而不是你的那个System.Windows.Forms.Timer,这是不一样的 TCP多线程,定时发送命令,程序运行100多次后自动停了,求解答 .net 4.0 WPF image图片锯齿 VS2005的问题,麻烦大家帮我解决下。。谢谢了 网站在本地访问正常,上传后出错 线程通信的问题 在64位机上部署.NET程序出错——请求高手帮忙!!! c#中如何通过web控件名得到该web控件对象 是中国人就进来看---上海反日大游行。老子没分散也要淹割 -- 苟日的劣等动物!!! string—实体属性映射 如何向某个最小化的程序发送按键(或组合键)? C# 如何得到richtextbox上的图片,并保存为图片文件 System.Media引用不了
PasswordChar是TextBox的属性 你找到这个属性 输入你要显示的符号 如果你输入的是* 那么文本内容都会替换成* 输入其它符号同意替换成你设置的符号
就是TextBox的啊 直接设*号就好了 多方便
呵呵,谢谢你
我刚才找半天没找到
我猜想起来我密码框用的是RichTextBox
难怪会没有
private void textBox1_TextChanged(object sender, EventArgs e)
{
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Tag = textBox1.Tag + textBox1.Text.Substring(textBox1.Text.Length - 1, 1);
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + "*";
textBox1.SelectionStart = textBox1.Text.Length; //设置光标到当前输入位置
timer1.Stop();
}
可以这样做 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox1.Text.Length>0 && textBox1.Text.Substring(textBox1.Text.Length - 1, 1) != "*")
{
e.Handled = true; //如果最后一位不是*,则取消本次(按键)动作
}
}
Timer timer1 = new Timer(); private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char ss = e.KeyChar;
if ((int)e.KeyChar != 8)
{
textBox1.Text = "".PadLeft(m_pwd.Length, '*') + e.KeyChar;
textBox1.SelectionStart = textBox1.Text.Length;
m_pwd += e.KeyChar;
timer1.Enabled = true;
e.Handled = true;
}
else
{
m_pwd = "";
textBox1.Text = "";
}
} private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = "".PadLeft(m_pwd.Length, '*');
textBox1.SelectionStart = textBox1.Text.Length;
timer1.Enabled = false;
}
{
this.textBox1.UseSystemPasswordChar = false;
//this.textBox1.Invalidate();
System.Threading.Thread.Sleep(1000);
this.textBox1.UseSystemPasswordChar = true;
}
/// 记录真实的密码
/// </summary>
private string realPass = null; private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 8 && realPass != null)
{
realPass = realPass.Substring(0, realPass.Length - 1);
return;
}
char currentChar = e.KeyChar;
realPass += currentChar;
//之所以用System.Threading.Timer,是因为这个timer是局部变量的时候
//只执行一次就销毁
System.Threading.Timer tm = new Timer(delegate
{
this.textBox1.Text = this.textBox1.Text.Replace(currentChar, '*');
}, null, 1, 0);
}
}
万分感谢这位大哥
经过我的测试之后
这位大哥的代码达到我想要的效果
就像用户输入的速度很快也不会出现问题
前面输入的都为'*'号
最后一个输入的显示1秒后才变成'*'号
只是代码我有点不怎么理解
1,Timer我是使用System.Timers命名空间,为什么大家写的代码中没有使用委托呢?
不是说在制定Timer时间到了要触发的方法时就会形成一个隐藏的线程吗?
我写的是这个:timers.Elapsed += new ElapsedEventHandler(AntherPasswordDisplay);
//AntherPasswordDisplay是我编写的一个方法的方法名,形参列表是
(object source, System.Timers.ElapsedEventArgs e)
不写这个会报错
整个方法为:
private void AntherPasswordDisplay(object source, System.Timers.ElapsedEventArgs e)
{
this.Invoke( (MethodInvoker) delegate
{
PassswordImputTextBox.Text = "".PadLeft(realPassWordString.Length, '*');
PassswordImputTextBox.SelectionStart = PassswordImputTextBox.Text.Length;
}); timers.Stop();
}
2,这位大哥在textBox1_KeyPress事件中写的第一句代码:
char ss = e.KeyChar;
这句有什么功能,我看全代码中都没有有关变量ss的使用
这个ss难道有什么特殊的用途吗?
这位大哥,你写的代码
我测试的时候
报错了
说:错误 1 “System.Windows.Forms.Timer”不包含采用“4”参数的构造函数 F:\Study\编程垃圾\测试\WindowsFormsApplication1\Form1.cs 37 41 WindowsFormsApplication1