rarahim Posted March 30, 2010 Posted March 30, 2010 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. Quote
Leaders snarfblam Posted March 30, 2010 Leaders Posted March 30, 2010 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. Quote [sIGPIC]e[/sIGPIC]
rarahim Posted March 31, 2010 Author Posted March 31, 2010 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? Quote
Administrators PlausiblyDamp Posted March 31, 2010 Administrators Posted March 31, 2010 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rarahim Posted March 31, 2010 Author Posted March 31, 2010 I have checked and the event handlers are indeed connected ti the events I have also removed the code which sets the MDIParent. Quote
rarahim Posted March 31, 2010 Author Posted March 31, 2010 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. Quote
Administrators PlausiblyDamp Posted March 31, 2010 Administrators Posted March 31, 2010 Tried it on a quick test project here and they fired just fine when the form was hidden, what version of .Net are you using? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
tchien69 Posted April 28, 2011 Posted April 28, 2011 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. Quote
bobbyKnight Posted May 2, 2011 Posted May 2, 2011 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 Quote
tchien69 Posted May 2, 2011 Posted May 2, 2011 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.