I am creating a Windows form application in C# using Visual Studio 2008.
I have a Button
control on my MainForm
, which opens AnotherForm
on Click
event.
What I want to do is:
- allow
AnotherForm
to be opened only once at a time, and - show an error message if the user tries to open another
AnotherForm
when it is already open, AND bring the already-openAnotherForm
to the front.
I was able to limit the AnotherForm
open count to 1 by using a static
field. However, I am having a hard time achieving requirement #2. It shows an error message, but it does not bring the already-opened AnotherForm
to the front.
Here is my code:
**MainForm**
private void BtnToOpenAnotherFornn_Click(object sender, EventArgs e)
{
AnotherForm af = new AnotherForm();
if (af.GetNumForms() < 1)
af.Show();
else
{
MessageBox.Show("AnotherForm is already open.");
//af.TopMost = true; //Not working
//af.BringToFront(); //Not working
}
}
**AnotherForm**
private static int NumForms = 0;
public int GetForms(){
return NumForms;
}
Can someone please tell me how to bring AnotherForm
to the front in the else
statement?