Preventing a form from opening more than once

Suppose you have a form named frmMain (Main) and a form named frmInformation (Information).  You want to open the frmInformation from frmMain as a modeless window (so the user can return to frmMain) but prevent the window from being opened again and having two copies of the Information window open.

On the button click or whichever trigger you choose in frmMain, use similar code to this:

// check to see if frmInformation is already open
if (Application.OpenForms["frmInformation"] == null)
{
     // it is not already open, so lets open it and show it.
     frmInformation informationForm = new frmInformation();
     informationForm.Show();
}
else
{
     // it was already open, so bring it into focus.
     Application.OpenForms["frmInformation"].Focus();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: