简体   繁体   English

我如何点击按键事件中的按钮

[英]How do i click button from keypress event

I want to click a button when i press F1我想在按 F1 时单击一个按钮

this is my code这是我的代码

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.F1)
            {
                buttonGfxIn_Click.PerformClick();
            }

        }

But i get this error但我收到这个错误

that button name Is a 'method' which is not valid in the given context error该按钮名称是在给定的上下文错误中无效的“方法”

I have test with a example and it is working well.我用一个例子进行了测试,它运行良好。 try it.尝试一下。

 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);

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM