Detecting Open Forms

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

Is there a way in CF .NET using C# to detect whether or not a form is
already open in memory, and if so, using a conditional to show that form
rather than having to do something like this:

Form1 frm = new Form1();
frm.Show();

since that always creates another instance. I don't want users to have a
bunch of the same form open on their PPC at one time, it's just silly and a
needless waste of resources.

Thanks
 
Aaron,

Make your forms implement the singleton pattern. Sample:

public class AaronsClass : System.Windows.Forms.Form
{
private static AaronsClass instance; // singleton

// singleton pattern
public static AaronsClass GetInstance()
{
if ( instance == null )
{
instance = new AaronsClass();
}
// optional - line below allows you to execute some code every time the
form's instance is gotten
// for example, if you are about to Show the form, you may want code in
the _initializeForm() method
// to reset controls to default values
//instance._initializeForm();
return instance;
}

So to show any form that implements the above pattern while only ever
instantiating it once, your code is:

AaronsClass.GetInstance().Show();

-Darren Shaffer
 
Keep a global instance instead of calling New all the time, then simply
check to see if the instance is Null or not.
 
I put that code into frmForm1, but when I press a button from frmForm1 that
opens up frmForm2, then press the "Back" button on frmForm2, then go back to
frmForm1, then go to frmForm2, then press the "Back" button again, I get the
following error...in other words, the second time I go into frmForm2 and
press the "Back" button is when this error occurs:

"An unhandled exception of type 'System.ObjectDisposedException' occurred in
System.Windows.Forms.dll"

Here's my back button OnClick event:

private void btnBack_Click(object sender, System.EventArgs e)

{

frmForm1.GetInstance().Show();

this.Close();

}

What is causing this, cause obviously after that error the program crashes?
 
The problem is when you Close() the form, you are not setting the Singleton instance variable to null. So, you have effectivly cheated Singleton by removing from memory what is supposed to stay in memory.

Before you call Close(), you could set your instance variable to null (you'd have to either make it public, or provide a public method/property to do so). Or, in the form that uses Singleton, hook the Closing event and make it set the instance variable to null.

This should cause it to reinstantiate itself the next time you call GetInstance();

*****************************************
* A copy of the whole thread can be found at:
* http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-compact-framework/9133
*
* Report spam or abuse by clicking the following URL:
* http://www.dotnetmonster.com/Uwe/Abuse.aspx?aid=b407af096035462786853fc3347975c6
*****************************************
 
All you need to do is Hide() your singleton forms instead of Close()ing
them.
Nulling the form instance should only be done if you know the form will not
be needed
again, because as Jordan says, you'll have to re-instantiate it, incurring
that expense
again.

-Darren
 
I would suggest adding the folowing to the AaronsClass:

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
instance = null;
}
 
Back
Top