Friday, May 14, 2010

Call a parent functions from child form



In order to call a parent function from a child form you have to pass the form1 instance to form2 in the constructor.


Form1:

namespace CallingForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 tmp = new Form2(this);
tmp.ShowDialog();
}

internal void msg()
{
MessageBox.Show("Hello from parent form");
}

}
}



Form 2:

namespace CallingForm
{
public partial class Form2 : Form
{
private Form1 frmParent;

public Form2(Form1 frm1)
{
InitializeComponent();
frmParent = frm1;
}

private void button1_Click(object sender, EventArgs e)
{
frmParent.msg();
}
}
}




No comments:

Post a Comment