|
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.- Marked As Answer byLinda LiuMSFT, ModeratorTuesday, March 03, 2009 8:09 AM
-
| | 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
- Marked As Answer byLinda LiuMSFT, ModeratorWednesday, March 04, 2009 5:48 AM
-
| | 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.- Marked As Answer byLinda LiuMSFT, ModeratorTuesday, March 03, 2009 8:09 AM
-
| | 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
- Marked As Answer byLinda LiuMSFT, ModeratorWednesday, March 04, 2009 5:48 AM
-
| | Linda Liu Tuesday, March 03, 2009 8:25 AM |
|