读书人

textBox1_TextChanged事件调用keychar

发布时间: 2013-08-04 18:26:16 作者: rapoo

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


}
}
}

读书人网 >C#

热点推荐