Keypress To Simulate A Button Click in C#

别等时光非礼了梦想. 提交于 2019-12-19 10:11:37

问题


Ok, so I'm in the process of making a Tic-Tac-Toe game to help me learn C#. I'm attempting to add a bit of functionality to it, so I want people to be able to use the NumPad on a computer to simulate clicking the buttons.

Here is what I have but when I use the NumPad the buttons don't click. Can any of you see a reason as to why?

    //===============================
    // start NumPad Simulate Clicks
    //   NumPad  MyButtons
    //   7 8 9   1 2 3
    //   4 5 6   4 5 6 
    //   1 2 3   7 8 9
    //===============================
    public void myControl_NumPad7(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad7)
        {
            button1_Click(null, null);
        }
    }
    public void myControl_NumPad8(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad8)
        {
            button2_Click(null, null);
        }
    }
    public void myControl_NumPad9(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad9)
        {
            button3_Click(null, null);
        }
    }
    public void myControl_NumPad4(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad4)
        {
            button4_Click(null, null);
        }
    }
    public void myControl_NumPad5(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad5)
        {
            button5_Click(null, null);
        }
    }
    public void myControl_NumPad6(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad6)
        {
            button6_Click(null, null);
        }
    }
    public void myControl_NumPad1(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad1)
        {
            button7_Click(null, null);
        }
    }
    public void myControl_NumPad2(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad2)
        {
            button8_Click(null, null);
        }
    }
    public void myControl_NumPad3(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad3)
        {
            button9_Click(null, null);
        }
    }

回答1:


EDIT
Noticed that I have to be clearer about what I mean...

From the code you posted I suspect you have 9 controls you added your key-events to. These controls will only receive key-events when they are focused.

To handle keys globally for the form, you have to set Form.KeyPreview to true. Also, I'd not handle the keys the way you do, but add a Form.KeyDown event and write something like:

switch (e.KeyCode)
{
    case Keys.NumPad9:
        e.Handled = true;
        button3.PerformClick();
        break;
    case Keys.NumPad8:
        e.Handled = true;
        button2.PerformClick();
        break;
    // And so on
}

This will handle the NumPad-Keys within the form - you can then remove all the event handlers you posted in your question.

To programatically "click" a button, you should use the Button.PerformClick() method, as more than one event handler may be added to the click event, which would not be called otherwise.

EDIT 2
Syntax for the switch-statement was invalid. Of course every "case" must start with the case keyword... Now it should work.




回答2:


You need to use button1.PerformClick(); for every button to invoke event correctly, here's info.



来源:https://stackoverflow.com/questions/1807483/keypress-to-simulate-a-button-click-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!