[英]How to make a keypress form event in C#
I'm trying to make a KeyPress event on a form, but in this line I got an error MainWindow.KeyPress = new KeyPressEventArgs(Form_KeyPress);
我正在尝试在表单上创建 KeyPress 事件,但在这一行中我收到一个错误
MainWindow.KeyPress = new KeyPressEventArgs(Form_KeyPress);
, I read the Microsoft Docs about events in C#, but I don't get the idea. ,我阅读了有关 C# 事件的 Microsoft Docs,但我不明白。
Do listeners like in Java exist in C# or not? C# 中是否存在像 Java 中的侦听器?
My code:我的代码:
class PracticeEvent
{
static void Main(String[] args)
{
Form MainWindow = new Form();
MainWindow.Text = "Practice";
MainWindow.MaximizeBox = false;
MainWindow.MinimizeBox = false;
MainWindow.FormBorderStyle = FormBorderStyle.FixedSingle;
MainWindow.StartPosition = FormStartPosition.CenterScreen;
MainWindow.Size = new Size(1000, 700);
MainWindow.KeyPreview = true;
MainWindow.KeyPress = new KeyPressEventArgs(Form_KeyPress);
MainWindow.ShowDialog();
}
private void Form_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.A){
MessageBox.Show("You pressed the A key.");
}
}
}
Your main method is static, your event handler is not.您的主要方法是静态的,您的事件处理程序不是。 You need to provide it an object reference, that's what the error message is trying to say.
您需要为它提供一个对象引用,这就是错误消息试图说明的内容。 Another error is that you are assigning rather than attaching the handler, use
+=
operator for that.另一个错误是您正在分配而不是附加处理程序,为此使用
+=
运算符。
Specifically, change this line:具体来说,改变这一行:
MainWindow.KeyPress = new KeyPressEventArgs(Form_KeyPress);
to be成为
var instance = new PracticeEvent();
MainWindow.KeyPress += instance.Form_KeyPress;
You have several errors in your code.您的代码中有几个错误。
MainWindow.KeyPress = new KeyPressEventArgs(Form_KeyPress);
1) KeyPress
has KeyPressEventHandler
type. 1)
KeyPress
具有KeyPressEventHandler
类型。 Not KeyPressEventArgs
.不是
KeyPressEventArgs
。 In C# classes which called ...EventArgs
are usually used as special objects that contains data about a raised event and them are inherited from EventArgs
system class.在 C# 中,称为
...EventArgs
类通常用作包含有关引发事件的数据的特殊对象,并且它们是从EventArgs
系统类继承的。 And classes which called ...EventHandlers
are usually define wrapper for delegates and called events.调用
...EventHandlers
类通常为委托和调用的事件定义包装器。
2) So KeyPress
is event. 2) 所以
KeyPress
是事件。 If you want to subscribe this event you should use +=
operator.如果你想订阅这个事件,你应该使用
+=
操作符。 And method that you want to specify as handler should have signature void(object, KeyPressEventArgs)
.您要指定为处理程序的方法应具有签名
void(object, KeyPressEventArgs)
。 Typical signature for events is void(object, ...EventArgs)
事件的典型签名是
void(object, ...EventArgs)
private void Form_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
3) As I said this method has wrong signature ( KeyPressEventArgs
instead KeyEventArgs
). 3)正如我所说,这个方法有错误的签名(
KeyPressEventArgs
而不是KeyEventArgs
)。
4) It should be static
. 4)它应该是
static
。 You can not use non-static class members in static method.不能在静态方法中使用非静态类成员。
So your code should look like this:所以你的代码应该是这样的:
class PracticeEvent
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Form MainWindow = new Form();
MainWindow.Text = "Practice";
MainWindow.MaximizeBox = false;
MainWindow.MinimizeBox = false;
MainWindow.FormBorderStyle = FormBorderStyle.FixedSingle;
MainWindow.StartPosition = FormStartPosition.CenterScreen;
MainWindow.Size = new Size(1000, 700);
MainWindow.KeyPreview = true;
MainWindow.KeyPress += Form_KeyPress;
MainWindow.ShowDialog();
}
private static void Form_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
{
MessageBox.Show("You pressed the A key.");
}
}
}
Use listeners in C# is not good practice, but some frameworks use it.在 C# 中使用侦听器不是一个好习惯,但一些框架使用它。 Usually events and callbacks is used.
通常使用事件和回调。
And my last advice.还有我最后的建议。 May be you want to use the
KeyDown
event?您可能想使用
KeyDown
事件吗? The KeyPress
is used for working with char input. KeyPress
用于处理字符输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.