简体   繁体   中英

Is it possible to trigger a click event from another form?

I need to run the code of a button that is on another form. is it possible to do it from a different form? if you say that it is possible by declaring it public then:

  1. how do i declare a control public?
  2. how do i pass the correct events into button_click ? it takes two parameters - how do i pass them,?

为什么不在共享类中创建一个click事件执行的公共方法。

It's possible to make a control in a Form public , but not recommended, you can do the following:

1) Declare a new event in the first form (form1) ButtonFirstFormClicked

public event EventHandler ButtonFirstFormClicked;

and trigger this event in the Button.Click event handler

void button_Clicked(object sender, EventArgs e)
{

    // if you're working with c# 6.0 or greater
    ButtonFirstFormClicked?.Invoke(sender, e);

    // if you're not then comment the above method and uncomment the below
    //if (ButtonFirstFormClicked!= null)
    //     ButtonFirstFormClicked(sender, e);
}  

2) In the second form (form2) subscribe the event

form1.ButtonFirstFormClicked += (s, e)
{
 // put your code here...
} 

Good luck!

You can use internal as your modifier, so you can easily access the click event.

for example you have a click event in form1. instead of making it private, public or protected. put it as internal this way you can easily access the method in other classes. But the internal modifier is only accessible within the current package.

Form1

internal passobj;
internal passeargs;

internal void button1_Click(object obj, EventArgs e)
{
 this.passobj = obj;
 this.passeargs = e;

 MessageBox.Show("Clicked!")
}

Form2

private void button1_Click(object obj, EventArgs e)
{

   Form1 f1 = new Form1();
   f1.button1_Click(f1.passobj, f1.passeargs);
}

In the form where the code is (firstForm) you will need to make the procedure public and avaliable to the secondary form where the secondary button is (btnMyButton). Once you have accomplished this you can hook up the click event hander of the secondary button to the code in the 1st form as follows.

Secondarily as stated above by Dustin you could opt to move this code into a separate class and then simply reference the method handler with as many events as you need.

Either way will work but I agree that if you want to follow good design you should have a separation of concerns as it relates to business logic (code) and presentation layer (ie forms with buttons)

//button in 2nd form

btnMyButton.Click += new EventHandler(firstForm.MethodThatHasCodeToRun);

Hope this helps,

Enjoy!

  1. You can make the control public by changing the "Modifiers" pseudo-property in the form designer.

  2. Once the button is public, you can run its Click event by calling the PerformClick method, eg form1.button1.PerformClick() . You don't have to call the event handler directly.

However, it might be a better idea to create a public method as Dustin Laine suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM