I am developing a windows based application in .net.
I have two forms. Form2 is instantiated and displayed when an event occurs in form1.
public partial class Form1 : Form
{
...
private void button1_Click(object sender, EventArgs e)
{
....
开发者_如何学Go }
public void button2_Click(object sender, EventArgs e)
{
Form2.parent = this;
Form2 f2 = new Form2();
f2.show();
}
}
public partial class Form2 : Form
{
...
public static Form parent;
private void button3_Click(object sender, EventArgs e)
{
.... //want to call Form1's button1_Click() function.
}
}
Now in Form2's button3_Click() function I want to call Form1's button1_Click() method.
I tried
parent.button1_Click(button3,null);
but this is not working.
please help!
You need to declare parent
as being of type Form1
rather than Form
.
I would personally put it in the constructor though, rather than having it as a public static variable:
public partial class Form1 : Form
{
public void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.show();
}
}
...
public partial class Form2 : Form
{
Form1 parent;
public Form2(Form1 parent)
{
this.parent = new parent;
}
}
精彩评论