In Form2 class, i declare an object of type Form1 like so:
public Form1 frmParent;
Then in a button click event of Form1 i have:
private void button4_Click(object sender, EventArgs e)
{
Form2 form2obj = new Form2();
form2obj.frmParent = this;
this.Hide(); /// You hide Form1, how do you close it later ?
form2obj.MdiParent = MdiParent;
form2obj.Show();
}
Then in Form1's FormClosing event i have:
(or is it Form2's closing ?)
frmParent is defined in the Form2 not Form1 ? It shouldn't compile. Also
Form1 is hidden. How do you close Form1 ?
if (frmParent != null)
frmParent.Close();
I would test a sample app with three buttons that would :
- button 1 : instantiate a new form
- button 2 : hide the previously instantiated form
- button 3 : close the privously hidden form
If you set a breakpoint in the instantiated form you'll see that those
events are called as expected...
Form1 contains :
Form2 frm;
private void button1_Click(object sender, EventArgs e)
{
frm = new Form2();
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
frm.Hide();
}
private void button3_Click(object sender, EventArgs e)
{
frm.Close();
}
Form2 contains :
private void FormClosingHandler(Object sender, FormClosingEventArgs
e)
{
MessageBox.Show("FormClosingHandler");
}
Click button1, button2, button3. The message is shown...