C# go to button function

Discussion in 'Programmer's Corner' started by Dritech, Aug 11, 2013.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi,

    I am using buttons in c# which I did shortcut keys from the keyboard. Now in the function of the keyboard shortcuts, I want the program to go the the actual button function.

    For instance I have a button called "Button_Bit_upDOWN" and this is it's function:

    Code ( (Unknown Language)):
    1. private void Button_Bit_upDOWN_Click(object sender, EventArgs e)
    2.         {
    3.  
    4.         }
    Q on the keyboard is its shortcut and this is it's function:

    Code ( (Unknown Language)):
    1.         private void Form_Home_KeyDown(object sender, KeyEventArgs e)
    2.         {
    3.             if(e.KeyCode == Keys.Q)
    4.             {
    5.                
    6.             }
    Now I included "Button_Bit_upDOWN_Click();" to the shortcut function so that when Q is pressed from the keyboard, the same function is Button_Bit_upDOWN_Click is done.

    Why is it not working please?

    Any kind of help is appreciated and sorry for my poor English.
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    So, is it safe to assume that you are using either Windows Forms or WPF, because you have not specified what you are using?

    With code, you cannot simply describe what you did, you must provide the code you tried. While you say you put the click function inside, how you did that is a matter of speculation.

    If you don't need it to be an event, why does it take the form:
    ?

    Just define your function as:
    Code ( (Unknown Language)):
    1. private void DoWhenButtonIsPressed()
    2. {
    3.     // TODO - what happens when the button is pressed
    4.  
    5. }
    6. private void Form_Home_KeyDown(object sender, KeyEventArgs e)
    7. {
    8.     if(e.KeyCode == Keys.Q)
    9.     {
    10.         DoWhenButtonIsPressed();
    11.     }
    12. }
    13.  
     
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi, I am using in Windows Form. I did as your last code buy instead of...:
    Code ( (Unknown Language)):
    1.  
    2. private void DoWhenButtonIsPressed()
    ...I used:
    Code ( (Unknown Language)):
    1.  
    2. private void DoWhenButtonIsPressed(object sender, EventArgs e)
    If I remove "object sender, EventArgs e" the program will give an error. Any help please?
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Okay, so, you are trying to get the same functionality from clicking a button to pressing 'Q', right?

    If that's the case, take everything from the working implementation and put it into a new, private function. Then, you can just call that function from each event function.
     
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the reply. So there is no way to go to the button function directly?
    Also, I am watching tutorials on youtube where most of them, them use the part of the program shown in the attached screenshot. How can I access this section please? This is not the coding area right? (I notices that his form is .vb and mine is .cs, why is that?)

    Thanks in advance
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    He would be using visual basic, where you are using C#... I'm pretty sure you can call the function from within the other, but you must specify the correct parameters. Have you tried simply passing the parameters from the key press to the button function? You will probably need to make a button pressed args (or whatever it is.. I'm not at my computer to check), to pass to the button pressed function... If I remember, I'll take a look tomorrow.....
     
  7. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    You don't need to do that and you can call the button method directly - I have done this loads. If it is not working then it is an error in the way you are calling it as tshuck says. Show us the full code.

    Use the debugger and put a breakpoint at the line you call the method and another in the button method itself (you do have code in there don't you?) and see if it really gets called.
     
  8. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Below is how I am doing it, but it is not working:

    Code ( (Unknown Language)):
    1.         private void Form_Home_KeyDown(object sender, KeyEventArgs e)  //If a key from the keyboard is pressed, enter this function
    2.         {
    3.             if(e.KeyCode == Keys.Q) //if the key is Q, enter here
    4.             {
    5.                 Button_UNlock_bit_Click();
    6.             }
    7.        }
    Code ( (Unknown Language)):
    1.         private void Button_UNlock_bit_Click(object sender, EventArgs e)
    2.         {
    3.             Button_up.Text = "a";
    4.         }
     
  9. vpoko

    Member

    Jan 5, 2012
    258
    47
    Calling an event handler without the event being raised can be done, but it's not a best practice and is discouraged. The best practice would be to put all your handling code in another method, and have both the button click and key press events call that method.

    But, if you insist, there are two options:

    You can call your button's PreformClick method. I.e., button_Bit_upDOWN.PerformClick(). This will raise the event, calling all registered event handlers for that button's click event.

    OR, you can call the method that's raised by the event, button_Bit_upDOWN_Click(), but don't forget that the method signature requires two arguments: one of type object, and one of type EventArgs. You can pass null references in their place, but be careful to check for those null references if you plan to use them in the method. Again, this is not a recommended practice and I would strongly advise going the separate method route.
     
    Dritech likes this.
  10. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    I'm wondering if it should even compile unless you have another method called
    Button_UNlock_bit_Click that takes no parameters. You need to call the method with all the arguments:

    Code ( (Unknown Language)):
    1.  
    2. if(e.KeyCode == Keys.Q) //if the key is Q, enter here {
    3.     Button_UNlock_bit_Click(sender, e);
    4. }
    5. ...
    6.  
    As vpoko says it would be tidier from a code reading point of view to put the actual code in a separate method and call it from both places.
     
    Last edited: Aug 13, 2013
    Dritech likes this.
  11. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the replies :)
     
Loading...