Windows Develop Bookmark and Share   
 index > Windows Forms General > How to prevent a Win Form from show up the second time
 

How to prevent a Win Form from show up the second time

Hi All,

I know that if I use form.ShowDialog(), users cannot click on my main menu to display the same form again, because they have to close the dialog first.

But I want to use form.Show() to display a form, because I want my users be able to go back to the application main menu to click on another menu item to display another form.

Is there a way to detect if a winform is already displayed so that I can prevent it from display again?

Thanks all in advance.
David N.
  • Moved bynobugzMVP, ModeratorFriday, February 27, 2009 7:34 PMnot a clr q (Moved from Common Language Runtime to Windows Forms General)
  •  
David N_2  Friday, February 27, 2009 6:20 PM
Just keep track of the form instance yourself so you know when there's one running. For example:

private Form2 mForm;

private void button1_Click(object sender, EventArgs e) {
if (mForm == null) {
mForm = new Form2();
mForm.FormClosing += new FormClosingEventHandler(mForm_FormClosing);
mForm.Show();
}
else {
// Already running, give it the focus
mForm.WindowState = FormWindowState.Normal;
mForm.Focus();
}
}

void mForm_FormClosing(object sender, FormClosingEventArgs e) {
mForm = null;
}

Moved to Windows Forms General.

Hans Passant.
nobugz  Friday, February 27, 2009 7:34 PM

Hi David,

If you set the main form as the owner of that form, you can access that form through the OwnedForms property of the main form. Ensure the title of that form is unique so that you can loop throughthe OwnedForms array to find if there's form with that title in it.

Alternatively, you can use Application.OpenForms collection.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

Linda Liu  Tuesday, March 03, 2009 8:25 AM

David,

there's nothing in windows.forms that does that for you. iow: incase you want this functionality, you must do it youself.

WM_SORRY
-thomas woelfer


http://www.die.de/blog
thomas_woelfer  Friday, February 27, 2009 7:33 PM
Just keep track of the form instance yourself so you know when there's one running. For example:

private Form2 mForm;

private void button1_Click(object sender, EventArgs e) {
if (mForm == null) {
mForm = new Form2();
mForm.FormClosing += new FormClosingEventHandler(mForm_FormClosing);
mForm.Show();
}
else {
// Already running, give it the focus
mForm.WindowState = FormWindowState.Normal;
mForm.Focus();
}
}

void mForm_FormClosing(object sender, FormClosingEventArgs e) {
mForm = null;
}

Moved to Windows Forms General.

Hans Passant.
nobugz  Friday, February 27, 2009 7:34 PM

Hi David,

If you set the main form as the owner of that form, you can access that form through the OwnedForms property of the main form. Ensure the title of that form is unique so that you can loop throughthe OwnedForms array to find if there's form with that title in it.

Alternatively, you can use Application.OpenForms collection.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

Linda Liu  Tuesday, March 03, 2009 8:25 AM

You can use google to search for other answers

Custom Search

More Threads

• Microsoft Office Like Help System
• the simplest question ever....
• How do I make the HireDate column be a Calendar column
• VC++ 2005 Designer Problem
• Treeview
• Mnemonic question
• transfer variables between forms
• Listbox Mouseleave ignores Scrollbar
• I have the same bug as one reported as fixed in MS Connect. How do I find the fix?
• How to load an image from local directory....