KeyPressEventArgs 的问题
这是我的问题..我正在用 C# 做一个计算器,我不想单击每个按钮来进行操作,我想用我的数字键盘来处理它..就像 如果我按“1”,则在文本框中显示“1”。
我改变了
private void cmd1_Click(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '1')
{
txtShow.Text='1';
}
}
,我遇到了这个错误:
“cmd1_Click”没有重载与委托“System.EventHandler”匹配
this.cmd1.Click += new System.EventHandler(this.cmd1_Click);
这到底是怎么回事?
干杯。
here's my problem .. i'm doing a calculator in C# and i don't want to click every single button to make a operation, i wanna handle it with my num pad .. like
if i press "1" , show me in the textbox "1".
i changed
private void cmd1_Click(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '1')
{
txtShow.Text='1';
}
}
and i'm having this error :
No overload for 'cmd1_Click' matches delegate "System.EventHandler"
this.cmd1.Click += new System.EventHandler(this.cmd1_Click);
what the hack is wrong with this?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改
为
您可能也想重命名
cmd1_Click
。正如上面的答案中提到的,这在表单本身而不是每个按钮上会更好。
change
to
You'll probably want to rename
cmd1_Click
too.And as mentioned in the answer above, this would be better on the Form itself, rather than each button.
您正在尝试将与
KeyPress
事件对应的事件处理程序附加到Click
事件。这里有问题(错误的复制/粘贴?):
它被命名为
cmd1
上的Click
事件的自动生成的事件处理程序,但其定义是一个 KeyPress 事件处理程序。您想处理哪个事件?
KeyPress
或Click
或两者兼而有之?You are trying to attach an event handler that corresponds to a
KeyPress
event to aClick
event.There is something wrong here (bad copy/paste?):
It's named as an auto-generated event handler for the
Click
event oncmd1
, but its definition is the definition for aKeyPress
event handler.Which event do you want to handle?
KeyPress
orClick
or both?单击是一个鼠标事件,如果您想接收键盘事件参数,您需要附加到键盘事件,您必须将所有计算器按钮放在一个公共面板中,并处理按钮单击“和”发送的文本到面板,这样您就可以对任何地方的两个按键做出反应并单击以获得相同的结果。
处理所有按钮的事件而无需逐一处理的一种简单方法是使用单个按钮单击处理程序并检查控件的文本属性以了解如何操作(将发送者转换为按钮并检查文本,执行一个开关)
未经测试:
Click is a mouse event, you need to attach to a keyboard event if you want to receive keyboard event args, you'd have to put all your calculator buttons in a common pannel and handle both the button click "and" the text being sent to the panel, that way you could react to both keypresses anywhere and to click for the same result.
An easy way to handling events for all the buttons without doing it one by one is to have a single button click handler and check the text property of the control to know how to act (cast the sender to a button and check the text, do a switch on that)
Not tested: