Click here to Skip to main content
16,020,257 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi, I'm trying to keep a form active but hidden rather than closing it. I think maybe I need to intercept the close message and change it, then call Hide() instead. The main (parent) form should dispose the child forms on exit. I think I understand the principles, but I don't know how to put it into practice

At the moment I have declared the child windows in the main form header, initialised them and Show() the appropriate form when the corresponding button is clicked. However when I close it, windows automatically disposes it and trying to un-hide it obviously doesn't work.

Any help much appreciated, but needed quite urgently.

This would be much easier if intellisense were working...
Posted
Updated 17-May-11 14:15pm
v4
Comments
Tarakeshwar Reddy 17-May-11 18:50pm    
I would suggest you remove the urgent from your message. Whenever people have time, they would look at your question and answer it. Adding urgent would just frustrate few people and you might not get an answer.
Nintynuts 17-May-11 19:33pm    
ok, done (though it's still quite urgent)
Mark Salsbery 17-May-11 19:46pm    
Have you tried handling the FormClosing event and in your handler setting the Cancel property of the eventargs to true? Then you could use Hide() or the Visible property of the form to hide it instead.

You'll probably never get intellisense in C++/CLI. C# is a better choice for managed UI programming....C++/CLI has been relegated to use for managed/unmanaged interop scenarios.
Nintynuts 17-May-11 19:54pm    
No I haven't but that sounds promising.
I'm pretty new to this so it would be great if you could give me example of how.
I found a FormClosing event in the form events list (I made a function by double clicking), I see an FormClosedEventArgs parameter (e), but my intellisense wont tell me it's members to edit them.
Mark Salsbery 17-May-11 20:07pm    
Just a quick architectural note: You'll need to subscribe to the event on the child form as you've already done, but where you handle the event depends on where it's more convenient. Since you are handling child form visibility and lifetime from the parent it may make more sense to handle the event in the parent form class.

Anyway, I was thinking something like this:

<pre> private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)
{
e->Cancel = true;
((Form ^)sender)->Hide();
}
</pre>

1 solution

Here is how to do it: override System::Windows::Forms::Form.OnFormClosing method:

C++
virtual void OnFormClosing(System::Object ^sender, System::Windows::Forms::FormClosingEventArgs ^e) {
   if (e->CloseReason == System::Windows::Forms::CloseReason::UserClosing) {
      Hide();
      e->Cancel = true;
    } //if
} //OnFormClosing


You don't need to worry about Dispose, it will be called as the form is kept under the Application.

[EDIT]
Overriding this method is certainly better than handling the event FormClosing. In the codelet shown above, Hide is actually "this->Hide()", so the instance is already passed to the method. With event, sender needs to be typecast to Form.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900