FormClosing() and FormClosed()

  • Thread starter Thread starter rarahim
  • Start date Start date
R

rarahim

Hi.

I have a hidden form in my app. When i call that form's Close() method from
another form to close it, the form's FormClosing() and FormClosed() events
are not raised. Is this behaviour normal and expected?

Tq.
 
Hello,
I have a hidden form in my app. When i call that form's Close() method
from another form to close it, the form's FormClosing() and FormClosed()
events are not raised. Is this behaviour normal and expected?

I don't see this behavior. They are raised (which is what I would expect).
How do you hide the form ? How do you know they are not raised ?

If I just instanciate the form I have the same behavior (the form is not
closed as it was never opened). Could it be what your code does ?
 
rarahim said:
Hi.

I have a hidden form in my app. When i call that form's Close() method
from another form to close it, the form's FormClosing() and FormClosed()
events are not raised. Is this behaviour normal and expected?

Tq.

As Patrice said, please provide more detail. Simple tests here (VS 2008 and
VS 2010 RC) show both events firing in simple VB Winform test apps. Exactly
what type of project are you working with and what VS version? Any other
unique circumstances?
 
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...
 
Back
Top