在控件有焦点的情况下按下键时发生。
[Visual Basic]
Public Event KeyPress As KeyPressEventHandler
[C#]
public event KeyPressEventHandler KeyPress;
[C++]
public: __event KeyPressEventHandler* KeyPress;
[JScript] 在 JScript 中,可以处理由某个类定义的事件,但不能定义自己的事件。
事件数据
事件处理程序接收一个 KeyPressEventArgs 类型的参数,它包含与此事件相关的数据。下列 KeyPressEventArgs 属性提供特定于此事件的信息。
备注
键事件按下列顺序发生:
- KeyDown
- KeyPress
- KeyUp
非字符键不会引发 KeyPress 事件;但非字符键却可以引发 KeyDown 和 KeyUp 事件。
要仅在窗体级别处理键盘事件并且不允许其他控件接收键盘事件,请将窗体的 KeyPress 事件处理方法中的 KeyPressEventArgs.Handled 属性设置为 true。
有关处理事件的更多信息,请参见使用事件。
.NET Framework 精简版平台说明: 除了 Control 基类外,.NET Compact Framework 还支持具有 Service Pack 2 及更高版本的控件上的该事件。Smart Devices Developer Community(智能设备开发人员团体)Web 站点提供了有关可用 Service Pack 的信息,请参见 http://go.microsoft.com/fwlink/?LinkId=16561。
示例
[Visual Basic]
' Boolean flag used to determine when a character other than a number is entered.
Private nonNumberEntered As Boolean = False
' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub textBox1_KeyDown()Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
' Initialize the flag to false.
nonNumberEntered = False
' Determine whether the keystroke is a number from the top of the keyboard.
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back Then
' A non-numerical keystroke was pressed.
' Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = True
End If
End If
End If
End Sub 'textBox1_KeyDown
' This event occurs after the KeyDown event and can be used
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress()Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
Handles textBox1.KeyPress
' Check for the flag being set in the KeyDown event.
If nonNumberEntered = True Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
End If
End Sub 'textBox1_KeyPress
End Class 'Form1
[C#]
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;
// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Initialize the flag to false.
nonNumberEntered = false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
}
// 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 (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}
}
[C++]
// Boolean flag used to determine when a character other than a number is entered.
private:
bool nonNumberEntered;
// Handle the KeyDown event to determine the type of character entered into the control.
void textBox1_KeyDown(Object* /**//*sender*/, System::Windows::Forms::KeyEventArgs* e)
{
// Initialize the flag to false.
nonNumberEntered = false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e->KeyCode != Keys::Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
}
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
void textBox1_KeyPress(Object* /**//*sender*/, System::Windows::Forms::KeyPressEventArgs* e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e->Handled = true;
}
}
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列
请参见
Control 类 | Control 成员 | System.Windows.Forms 命名空间 | OnKeyPress