Open existing Form

462 views Asked by At

I have this code:

  private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
  {
      Form8 of = new Form8();
      of.ShowDialog();
  }

That button opens my form8 (good). But if I click twice, the form duplicates, so I got 2 same form8.

Does any body knows how to just select (bring to front) the form8 if it is already open, when I click the linklabel for second time. Thanks!

1

There are 1 answers

4
Jon On
public class MainForm
{
    // Keep a reference to your popup form here, so you never create more than one instance
    private Form8 of = new Form8();

    private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
    {
        if (of != null && of.IsDisposed)
            of = new Form8();

        // Call Show(), not ShowDialog() because ShowDialog will block the UI thread
        // until you close the dialog.
        of.Show();
        of.BringToFront();
    }
}