我在 C# 中找不到 KeyPress 事件处理程序
I can not find KeyPress event handler in c#
我是 c# 新手。我正在尝试制作一个 windows 10 应用程序,其中我有一个仅接受数字和一位小数的文本框。我在这里看到很多人说要使用 KeyPress 事件处理程序,但我没有。我只有 KeyDown 和 KeyUp。我看到有人 post 将以下代码与 KeyDown 一起使用:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key < Key.D0 || e.Key > Key.D9)
{
e.Handled = true;
}
}
但即便如此,我仍收到 Key.D0 和 Key.D9 的错误 "Key does not exist in the current context"。我完全不知所措,如果有人能提供帮助那就太好了。
假设这是一个 WinForm 应用程序,请参阅 MSDN 上的以下 Control.KeyPress Event
示例(回复:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx)
// 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 condition
if (SOME CONDITION)
{
// Stop the character from being entered into the control.
e.Handled = true;
}
}
希望这可能有所帮助。
您可以使用自定义代码手动生成此事件。我希望你明白了。
public YourFormName()
{
InitializeComponent();
this.KeyPress -= YourFormName_KeyPress;
this.KeyPress += YourFormName_KeyPress;
}
private void YourFormName_KeyPress(object sender, KeyPressEventArgs e)
{
//Check for any key you want.
if (e.KeyChar == (char)Keys.Enter)
{
//do anything.
}
}
假设 "Windows 10 app" 是指 "Universal App",您可以在名为 TextBox_KeyDown
的方法中使用以下代码,该方法与文本框的 KeyDown
事件相关联.
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if(e.Key < Windows.System.VirtualKey.Number0 || e.Key >= Windows.System.VirtualKey.Number9)
{
e.Handled = true;
}
}
我是 c# 新手。我正在尝试制作一个 windows 10 应用程序,其中我有一个仅接受数字和一位小数的文本框。我在这里看到很多人说要使用 KeyPress 事件处理程序,但我没有。我只有 KeyDown 和 KeyUp。我看到有人 post 将以下代码与 KeyDown 一起使用:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key < Key.D0 || e.Key > Key.D9)
{
e.Handled = true;
}
}
但即便如此,我仍收到 Key.D0 和 Key.D9 的错误 "Key does not exist in the current context"。我完全不知所措,如果有人能提供帮助那就太好了。
假设这是一个 WinForm 应用程序,请参阅 MSDN 上的以下 Control.KeyPress Event
示例(回复:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx)
// 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 condition
if (SOME CONDITION)
{
// Stop the character from being entered into the control.
e.Handled = true;
}
}
希望这可能有所帮助。
您可以使用自定义代码手动生成此事件。我希望你明白了。
public YourFormName()
{
InitializeComponent();
this.KeyPress -= YourFormName_KeyPress;
this.KeyPress += YourFormName_KeyPress;
}
private void YourFormName_KeyPress(object sender, KeyPressEventArgs e)
{
//Check for any key you want.
if (e.KeyChar == (char)Keys.Enter)
{
//do anything.
}
}
假设 "Windows 10 app" 是指 "Universal App",您可以在名为 TextBox_KeyDown
的方法中使用以下代码,该方法与文本框的 KeyDown
事件相关联.
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if(e.Key < Windows.System.VirtualKey.Number0 || e.Key >= Windows.System.VirtualKey.Number9)
{
e.Handled = true;
}
}