我想在函数中调用按钮事件,如下所示,这可能不是正确的方法。如果你知道如何解决这个问题,请分享一些如何实现它的样本。 感谢
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Down:
button4_Click(sender, e); //error: sender and e does not exist in the current context
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
答案 0 :(得分:2)
试试这个:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Down:
button4_Click(null, null);
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
答案 1 :(得分:1)
为什么不将button4_Click
代码移动到ProcessSomething()
等其他方法并在那里进行处理。然后只需更改
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Down:
ProcessSomething();
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
您可以通过向其传递空值来触发button4_Click
方法,但是IMO对此类事件进行触发并不优雅。
答案 2 :(得分:0)
调用button4 click
将自己作为发件人传递,将新System EventArgs
作为第二个参数传递:
button4_Click(button4, new System.EventArgs());