views:

106

answers:

4

What's a good technique to create a WinForms Form instance, display it (non-modally) but not have to keep a reference around to it? Normally, as soon as the variable goes out of scope, the form is closed:

var form = new SuperDuperForm();
form.Show();
// Form has probably been closed instantly

I don't want to have to keep track of instances of the form, I want it so that when the user closes the form, it is disposed. One idea I've had that I'm going to implement is a kind of controller that I use to open and display forms, that will keep track of them and monitor when they are closed via callbacks.

I'm just wondering if there are any neat tricks to get away without that. Any ideas?

+1  A: 

I have no problems opening forms without keeping references to them. However, if you create the the form within a separate thread, it will vanish when the thread exits (which it will do immediately, since Show returns as soon as the form is displayed).

Fredrik Mörk
A: 

I think that you can instance a form without keeping a reference. One technique I have used is keeping track through a Dictionary myForms<sting, Form> collection. This meant that I could still subscribe to a .FormClosing event and get rid of the form in my collection.

Of course, if you want to do it the right way, you would use Application.OpenForms - see Aaronaught's answer. I only just found out about that property after reading his answer.

dboarman
A: 

What about a simple "using" clause? Something like:

using (Form vForm = new Form()} { // your code goes here )

The instance is dismissed once you go out of scope of the "using". There is also the cheat of a simple call such as (new Form()).Show(); A quick test shows me a window that doesn't go away when the calling method closes.

Bruce
People, if you are going to downcheck an answer, you should include comments as to why the downcheck.
Bruce
+4  A: 

You don't have to do this at all - Winforms does it for you. When you show a form, it gets added to the Application.OpenForms, property, and when you close the form, it is automatically removed.

A Windows Form will never be eligible for garbage collection while it is open, and will also automatically be disposed when it is closed. Quoting from the Form.Close documentation:

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

In other words, for the general case when you "fire and forget", resource management is automatic.

Aaronaught
+1 Great, thanks. I have no idea what my problem was, then. I thought I'd had a situation where the form just closed straight away, but that was a little while back and I must be mistaken. Thanks again.
Neil Barnwell
+1 @Aaronaught: Good to know - I had no idea OpenForms was there. I guess I don't need to keep my own collection for forms then.
dboarman