User1979
User1979

Reputation: 857

How to call a button click event from another method

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().
}

Upvotes: 43

Views: 303442

Answers (15)

Marco Concas
Marco Concas

Reputation: 1892

For WPF:

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

Upvotes: 1

hamedSalarvand
hamedSalarvand

Reputation: 1

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);
    }

Upvotes: 0

FelippeKalil
FelippeKalil

Reputation: 21

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

SubGraphButton_Click(null, RoutedEventArgs.Empty);

Upvotes: 2

Rory
Rory

Reputation: 41

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.

Upvotes: 0

Renato Farias
Renato Farias

Reputation: 460

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

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

Upvotes: 6

Pedram
Pedram

Reputation: 2611

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

btnButton.PerformClick();

Upvotes: 98

Volkan Yurtseven
Volkan Yurtseven

Reputation: 444

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

Upvotes: 2

patel.milanb
patel.milanb

Reputation: 5982

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);
}

Upvotes: 82

user6648617
user6648617

Reputation: 21

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);
        }
    }

Upvotes: 2

Aphonk Rofest
Aphonk Rofest

Reputation: 31

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);"
    }
}

Upvotes: 3

Dark Knight
Dark Knight

Reputation: 888

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());
}

Upvotes: 21

Nima Soroush
Nima Soroush

Reputation: 12814

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();
}

Upvotes: 8

m.edmondson
m.edmondson

Reputation: 30862

Add it to the instance of the Click delegate:

ChildNode.Click += SubGraphButton_Click

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

Upvotes: 0

Germann Arlington
Germann Arlington

Reputation: 3353

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

Upvotes: 4

zmbq
zmbq

Reputation: 39013

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.

Upvotes: 2

Related Questions