locked
How to click on a button by pressing "Enter" in the keyboard when the cursor position is already on the button? RRS feed

  • Question

  •    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if(e.KeyChar == (char) 13)
                {               
                    Cursor.HotSpot = SendKeys.Send("{ENTER}");

                }


            }

    Above is the code i fail to continue, i need some urgent help please

                                    
    Thursday, July 21, 2016 6:54 AM

Answers

  • Hi,

    I am just wondering why you want to get the button pressed?

    The core requirement (if I understood your issue correctly) is more or less:
    The button click event is handled somewhere and has some code in it. The same code should be done now at that place.

    So from my understanding there is some kind of refactoring required:
    - The code from the button click goes to its own method.
    - This method is now called by the Key press and the button click event handlers.

    The core reason why I tried to find such a solution instead is simply, that you should try to divide your code a little bit. You should have the UI on one side and then the core business logic (so what is really happening) on the other side. Both parts should be connected as loose as possible (e.g. through bindings).

    So the core logic that is going on should even be outside of the form.

    But of course: You could also click the button in your code and I do not want to hide that from you:

    The Button has a PerfomClick method which you could call.

    If your problem is, that you do not know, which control has the focus: You can use the ActiveControl property to find the control. But be aware that controls can be set up in a tree form and you always just get the form on the next level. So you might use a recursion or loop to ask for ActiveContol till you find an control that is not a container.

    With kind regards,

    Konrad

    Thursday, July 21, 2016 9:26 AM
  • Does the button have to have focus? 

    If not, just set the Form's AcceptButton property to the Button you want ti "click" and the Enter key will have this affect no matter where the focus is within the Form (except, IIRC, if it's in a multi-line TextBox). 


    Regards, Phill W.

    Thursday, July 21, 2016 11:26 AM

All replies

  • Hi,

    I am just wondering why you want to get the button pressed?

    The core requirement (if I understood your issue correctly) is more or less:
    The button click event is handled somewhere and has some code in it. The same code should be done now at that place.

    So from my understanding there is some kind of refactoring required:
    - The code from the button click goes to its own method.
    - This method is now called by the Key press and the button click event handlers.

    The core reason why I tried to find such a solution instead is simply, that you should try to divide your code a little bit. You should have the UI on one side and then the core business logic (so what is really happening) on the other side. Both parts should be connected as loose as possible (e.g. through bindings).

    So the core logic that is going on should even be outside of the form.

    But of course: You could also click the button in your code and I do not want to hide that from you:

    The Button has a PerfomClick method which you could call.

    If your problem is, that you do not know, which control has the focus: You can use the ActiveControl property to find the control. But be aware that controls can be set up in a tree form and you always just get the form on the next level. So you might use a recursion or loop to ask for ActiveContol till you find an control that is not a container.

    With kind regards,

    Konrad

    Thursday, July 21, 2016 9:26 AM
  • Does the button have to have focus? 

    If not, just set the Form's AcceptButton property to the Button you want ti "click" and the Enter key will have this affect no matter where the focus is within the Form (except, IIRC, if it's in a multi-line TextBox). 


    Regards, Phill W.

    Thursday, July 21, 2016 11:26 AM