C# Access Method of Form1 on Form2

后端 未结 4 972
生来不讨喜
生来不讨喜 2021-01-16 21:42

I have 2 Forms in my project. Form1 is the main Form. There I have a button to open Form2, a ListView and a method to call a url and feed the ListView with data it gets from

4条回答
  •  别那么骄傲
    2021-01-16 22:00

    Have a look:

    //In Form1 opening Form2
    Form2 frm = new Form2();
    frm.Owner = this;
    frm.Show();
    
    //Example to call methods in FORM1 from FORM2
    private void button1_Click(object sender, EventArgs e)
    {
        Form1 frmParent = (Form1)this.Owner;
        frmParent.testeFunction();
        frmParent.InsertInGrid(textBox1.Text);
    }
    

    So, basically you need to create one function in Form1 to call from Form2 (passing Parameters). I hope this helps.

提交回复
热议问题