首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

textBox1_TextChanged事件调用keychar如何报错,怎么修改

2013-08-04 
textBox1_TextChanged事件调用keychar怎么报错,如何修改 private void textBox1_TextChanged(object sende

textBox1_TextChanged事件调用keychar怎么报错,如何修改
 private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
           if (e.Keychar== (char)13)   //(char)13 回车键
                if (textBox1.Text.ToLower().Trim() != "admin")
                {
                    this.textBox1.Text = "";
                    this.textBox1.Focus();
                    MessageBox.Show("用户名错误");
                } 
        }
我知道是说system.EventArgs不包含keychar,而在keypressEventArgs包含,修改后报错其也不包含,怎么修改呢?
[解决办法]
用TextChanged事件是不行的,它的参数总是EventArgs类型,强制转换也没用。
按照你的需求,需要用KeyPress或者KeyDown事件。
[解决办法]

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox1.Text.ToLower().Trim() != "admin")
                {
                    this.textBox1.Text = "";
                    this.textBox1.Focus();
                    MessageBox.Show("用户名错误");


                }
            }
        }

热点排行