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

前端 未结 5 891
北荒
北荒 2020-12-30 07:38

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:

<
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 08:12

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

提交回复
热议问题