C# go to button function

Thread Starter

Dritech

Joined Sep 21, 2011
901
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:

Rich (BB code):
private void Button_Bit_upDOWN_Click(object sender, EventArgs e)
        {

        }
Q on the keyboard is its shortcut and this is it's function:

Rich (BB code):
        private void Form_Home_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Q)
            {
                
            }
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.
 

tshuck

Joined Oct 18, 2012
3,534
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:
private void Button_Bit_upDOWN_Click(object sender, EventArgs e)
{

}
?

Just define your function as:
Rich (BB code):
private void DoWhenButtonIsPressed()
{
    // TODO - what happens when the button is pressed

}
private void Form_Home_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Q)
    {
        DoWhenButtonIsPressed();
    }
}
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi, I am using in Windows Form. I did as your last code buy instead of...:
Rich (BB code):
private void DoWhenButtonIsPressed()
...I used:
Rich (BB code):
private void DoWhenButtonIsPressed(object sender, EventArgs e)
If I remove "object sender, EventArgs e" the program will give an error. Any help please?
 

tshuck

Joined Oct 18, 2012
3,534
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.
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
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
 

Attachments

tshuck

Joined Oct 18, 2012
3,534
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.....
 

sirch2

Joined Jan 21, 2013
1,037
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.
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.
 

Thread Starter

Dritech

Joined Sep 21, 2011
901
You don't need to do that and you can call the button method directly - I have done this loads.
Below is how I am doing it, but it is not working:

Rich (BB code):
        private void Form_Home_KeyDown(object sender, KeyEventArgs e)  //If a key from the keyboard is pressed, enter this function
        {
            if(e.KeyCode == Keys.Q) //if the key is Q, enter here
            {
                Button_UNlock_bit_Click();
            }
       }
Rich (BB code):
        private void Button_UNlock_bit_Click(object sender, EventArgs e)
        {
            Button_up.Text = "a";
        }
 

vpoko

Joined Jan 5, 2012
267
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.
 

sirch2

Joined Jan 21, 2013
1,037
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:

Rich (BB code):
if(e.KeyCode == Keys.Q) //if the key is Q, enter here {
    Button_UNlock_bit_Click(sender, e);
}
...
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:
Top