日期:2014-05-17  浏览次数:21072 次

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("用户名错误");