本人不才,是新手
用VS2008
最近编一个窗体,密码框我想有这样的功能:
比如当用户输入密码6时,密码框会显示6,大概一秒吧
然后马上变成"*"星号
请问这该怎么做?
谢谢了.

解决方案 »

  1.   

    在OnChange事件中start一个timer,然后1秒后,将最后一个字符设置为*,但实际的密码内容存储在tag中
      

  2.   

    直接设PasswordChar属性为*好了  谁输密码时还来得及看看自己输得是什么啊
      

  3.   

    请问PasswordChar属性是TextBox的吗?还是Form的?
      

  4.   


    PasswordChar是TextBox的属性    你找到这个属性 输入你要显示的符号 如果你输入的是* 那么文本内容都会替换成*   输入其它符号同意替换成你设置的符号
      

  5.   


    就是TextBox的啊  直接设*号就好了  多方便
      

  6.   


    呵呵,谢谢你
    我刚才找半天没找到
    我猜想起来我密码框用的是RichTextBox
    难怪会没有
      

  7.   


            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();
            }
      

  8.   

    timer的interval属性设为了1000你可以根据实际情况加长或减短
      

  9.   

    这个可以改变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;   //如果最后一位不是*,则取消本次(按键)动作
                }
            }
      

  10.   

    1楼正解先向窗体中加入一个timer控件,再为timer加入Tick事件,并设置间隔时间为1000毫秒,将要实现的效果代码写在Tick事件中就可以了;再为密码控件加入OnChange事件,在这个事件里调用timer的Start()方法即可,注意每执行一次Tick事件后将Timer关了
      

  11.   

            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;
            }
      

  12.   

    你试试看这段代码,在 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;
            }
      

  13.   

    /// <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);
            }
        }
      

  14.   

    你这样不准确,有可能刚输入一个字符的时候就到了timer的周期了
      

  15.   


    万分感谢这位大哥
    经过我的测试之后
    这位大哥的代码达到我想要的效果
    就像用户输入的速度很快也不会出现问题
    前面输入的都为'*'号
    最后一个输入的显示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难道有什么特殊的用途吗?
      

  16.   


    这位大哥,你写的代码
    我测试的时候
    报错了
    说:错误 1 “System.Windows.Forms.Timer”不包含采用“4”参数的构造函数 F:\Study\编程垃圾\测试\WindowsFormsApplication1\Form1.cs 37 41 WindowsFormsApplication1
      

  17.   

    char ss = e.KeyChar;  这个删掉 是误打 不好意思
      

  18.   

    帅锅,我都写了是System.Threading.Timer,而不是你的那个System.Windows.Forms.Timer,这是不一样的