Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to fire click event for a button from a nother control

neeed help
Posted

In a very crude way, you can just call the original event handler just like any method. Foe e.g. I am calling the button clicked event also from checkbox changed:

C#
private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Button Clicked!!!");
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    button1_Click(sender, e);
}


However, a better way would be look back and see what you actually want. And perhaps you can implement a new common custom event which can fired by multiple controls.
 
Share this answer
 
Comments
beljk 20-Apr-14 9:22am    
i want do it but from another form , not current form
You can do it - each button has a PerformClick method[^] you can call, which is the equivalent of clicking it with the mouse - buit I'd say it is generally poor design to use it, particularly from outside the actual UserControl or Form that houses the button - it "locks" the two classes together so you can't change one without considering the effects on the other, and it measn you have to make teh button public, which is against OOPs principles.

A better way is to create a method within the Form / UserControl with is public and which can be called by the outside world which does the same thing as clicking the button. That way the button can be replaced with a different control without the outside world needing to know about it.
 
Share this answer
 
Another way, create a public method

C#
//Form1

public void SimulateClick()
{
    button1_Click(null, null);
}



//Form2

form1.SimulateClick();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900