我想在按F1时单击一个按钮
这是我的代码
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
buttonGfxIn_Click.PerformClick();
}
}
但是我得到这个错误
该按钮名称是在给定上下文错误中无效的“方法”
答案 0 :(得分:1)
我通过一个示例进行了测试,并且运行良好。试试吧。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1 was clicked");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
KeyEventArgs e = new KeyEventArgs(keyData);
if (e.KeyCode == Keys.F1)
{
button1_Click("", e);
}
return base.ProcessCmdKey(ref msg, keyData);
}
}