Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • Leaders
Posted
This behavior doesn't surprise me. If the form is never shown, you probably wont get a Form.Load event since this is called immediately before the form is shown. A lot of initialization gets done the first time a form is shown, and if it is never shown, the normal clean-up probably doesn't need to be performed either, so like Form.Load, Form.Closed probably wont be raised. It seems understandable since you really aren't closing the form, although this isn't immediately obvious if you aren't very familiar with the winforms infrastructure.
[sIGPIC]e[/sIGPIC]
Posted

The hidden form was indeed visible initially, then set to invisible.

 

Actually i have 2 forms, Form1 and Form2. 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();

 

form2obj.MdiParent = MdiParent;

form2obj.Show();

}

 

Then in Form1's FormClosing event i have:

 

if (frmParent != null)

frmParent.Close();

 

I place some clean up code in Form2's FormClosing (and FormClosed) event but the event never gets called.

 

What is wrong with this scenario?

  • Administrators
Posted

Are you sure the event handlers are connected to the events themselves? If you used the designer to generate them it should have attached the event handler in the form1.designer.cs file; otherwise you will need to explicitly wire the event handlers together e.g.

       public Form1()
       {
           InitializeComponent();
           FormClosed+=Form1_FormClosed;
           FormClosing+= Form1_FormClosing;
       }

Out of curiosity is there a reason you are setting the MDIParent property and also adding the frmParent variable? It seems the frmParent could always be found by checkingthe MDIParent property.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

OK, when I commented out the line that hides Form1, i.e.

 

private void button4_Click(object sender, EventArgs e)

{

Form2 form2obj = new Form2();

form2obj.frmParent = this;

//this.Hide();

 

form2obj.MdiParent = MdiParent;

form2obj.Show();

}

 

the FormClosing event finally fires.... this is why I've been suspecting that FormClosing and FormClosed events won't fire if a form is hidden but I could not find this being mentioned anywhere in the MSDN.

  • 1 year later...
Posted

I have the same problem. Visual Studio 2008, .NET 3.5, VB.NET, WinForms 3.5 on XP SP2.

 

I have an MDI App where the User opens a data entity represented by multiple Child Forms. He is allowed to Hide one or more of these Child Forms and then close that entity at which point the App calls Form.Close on all the Loaded Child Forms Visible / Hidden or not. For the in-Visible (Hidden) ones, the FormClosing and FormClosed Events are not firing.

Posted

This behavior is most likely because you are missing the "handles form event"...

 

example....

 

wrong:

 

Private Sub Form1_FormClosing1(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)

 

correct:

 

Private Sub Form1_FormClosing1(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

Posted

I do have they "Handles" Clause on my Sub declaration.

 

Besides, like the O.P. and I said, the Event Handlers are not being called only

when the Form is in-Visible (i.e. Form.Visible = False) when its Close Method is called. If I were missing "Handles" Clause, it would never be called regardless of whether the Form is in-Visible.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...