Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

c# - preventing multiple instance of one form from displaying

I am working on an application in which there is one main form and several other forms that can be used concurrently. when A user clicks to open another form, I'd like to make it so that clicking the button for the form does not open the form up again if it is already open.

showDialog wont work because the user still needs to have access to the controls on the main form.

here is my code for the help window, all the other forms open the same way.

private void heToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form help = new help();
            help.Show();
        } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Alternatively you can use the Application's open forms to see if it is open

btn_LaunchHelp(object o, EventArgs e)
{

   foreach (Form f in Application.OpenForms)
   {
       if (f is HelpForm)
       {
           f.Focus();
           return;
       }
   }

   new help().Show();
}

Edit: To be more clear, this allows the user to close the Help anytime and makes is much easier to manage than saving a reference to the Help window. Nothing to clean up, nothing to maintain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...