如果用户点击回车键,我想显示一条消息,说明输入键已被点击。否则,我想正常输入文本框。
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Was Clicked");
}
}
如果按下Enter键,则会正确显示消息。但是,如果我尝试输入一个句子,文本框中就不会出现任何内容。
这会有用......
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Was Clicked");
}
else
{
MessageBox.Show(e.KeyCode.ToString());
}
}
但是,我不想要一个显示每个输入字母的信息框。
答案 0 :(得分:2)
试试这个:
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(e.KeyCode.ToString());
}
else
{
e.Handled = true;
}
}
请工作!这是我遇到的最令人困惑的事情!
答案 1 :(得分:0)
使用KeyPress
事件:
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (e.KeyChar == (char)Keys.Return)
{
// Stop the character from being entered into the control since it is non-numerical.
MessageBox.Show("Enter Was Pressed");
e.Handled = true;
}
}