43

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method?

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   // call SubGraphButton-Click().
}
0

15 Answers 15

97
+50

You can easily do it by the following piece of code (assuming that name of your button is btnButton):

btnButton.PerformClick();
Sign up to request clarification or add additional context in comments.

8 Comments

I used to always use this, but after having a lot of issues with this, i think the chosen answer is a much more secure way to go.
@Pedram No intention to upset you, its just the PerformClick() method for a button won't fire if the button is not Enabled and Visible, most of the forms i make i usually enable and disable buttons hence I had issues with it.
@Burgo855 Thanks for your feedback! So it depends on the kind of call or where you want to use it. Generally speaking, it does call the button. And, be sure that all ideas are appreciated here and no one becomes upset just because you share your idea :)
This doesn't always work if it's inside something that will create cross threading. This a hack to fix that issue. Might help somebody else: this.Invoke((MethodInvoker)delegate () { btnButton.PerformClick(); });
I wonder why I spend 2 hours to try my way , but not found this answer XD
|
82

You can call the button_click event by simply passing the arguments to it:

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   SubGraphButton_Click(sender, args);
}

3 Comments

What if SubGraphButton_Click() is async? And the method that needs to simulate the click isn't? This isn't the correct answer. The correct answer would be to call SubGraphButton.PerformClick() inside ChildNode_Click()
probably add "await" before the call
For anyone wanting to do this outside of an event, I manged by creating a custom sender and args: object sender = this; and RoutedEventArgs args. Then call it like normal: SubGraphButton_Click(sender, args);
21

you can call the button_click event by passing..

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   SubGraphButton_Click(sender, args);
}

Also without passing..

private void SubGraphButton_Click(object sender, EventArgs args)
{
}

private void Some_Method() //this method is called
{
   SubGraphButton_Click(new object(), new EventArgs());
}

Comments

8

You can perform different approaches to work around this. The best approach is, if your both buttons are suppose to do the same job, you can define a third function to do the job. for example :

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
    myJob()  
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
    myJob()
}

private void myJob()
{
    // Your code here
}

but if you are still persisting on doing it in your way, the best action is :

private void SubGraphButton_Click(object sender, RoutedEventArgs args)
{
}

private void ChildNode_Click(object sender, RoutedEventArgs args)
{
   SubGraphButton_Click.PerformClick();
}

Comments

6

In WPF, you can easily do it in this way:

this.button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

Comments

4

Usually the better way is to trigger an event (click) instead of calling the method directly.

1 Comment

Your answer would be improved if you added an example code segment that demonstrates what you mean.
3
private void PictureBox1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Click Succes");
}

private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13)
    {
        PictureBox1_Click(sender, e); //or try this one "this.PictureBox1_Click(sender, AcceptButton);"
    }
}

Comments

2

You can simply call it:

SubGraphButton_Click(sender, args);

Now, if your SubGraphButton_Click does something with the args, you might be in trouble, but usually you don't do anything with them.

Comments

2

For me this worked in WPF

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent, Button_OK);
            Button_OK.RaiseEvent(routedEventArgs);
        }
    }

Comments

2

Use InvokeOnClick event. it works even if the button is invisible/disabled

Comments

2

A simple way to call it from anywhere is just use "null" and "RoutedEventArgs.Empty", like this:

SubGraphButton_Click(null, RoutedEventArgs.Empty);

Comments

1

For WPF:

YourButtonName.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));

Comments

0

Add it to the instance of the Click delegate:

ChildNode.Click += SubGraphButton_Click

which is inkeeping with the pattern .NET events follow (Observer).

Comments

0

For people wondering, this also works for button click. For example:

private void btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test")
        }

private void txb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btn_Click(sender, e);
            }

When pressing Enter in the textfield(txb) in this case it will click the button which will active the MessageBox.

Comments

0

we have 2 form in this project. in main form change

private void button1_Click(object sender, EventArgs e) { // work }

to

public void button1_Click(object sender, EventArgs e) { // work }

and in other form, when we need above function

        private void button2_Click(object sender, EventArgs e)
    {
        main_page() obj = new main_page();
        obj.button2_Click(sender, e);
    }

1 Comment

Thank you for sharing an answer. However, it obfuscates the reason why it works. In your case, you needed to make the event handler public. This is a different solution to needing to pass in two parameters to the first method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.