How to open only one form even by multiple clicking C#

Asked

Viewed 134 times

-1

Good afternoon, I’m doing a quiz on Windows Form,:

 Async void Proximo()

{

  await Task.Delay(100);

  this.Close();

  Form2 form = new Form2;

  form.Show();

}

private void button1_Click(object sender , EventArgs e)   

 {            
                                                       Proximo() ;      
                             }

Only if I give a double/triple...click, will open two/three times the same form.How do I solve this? Even giving a triple click open only a single form?

  • 1

    should not use async just to open another form, minus a delay to delay something... you can also use Showdialog that makes the form open in a restricted way and return a Dialogresult...

3 answers

2

Basically you would have to check if the form is already open:

if (Application.OpenForms.OfType<Form2>().Count() > 0)
{
    MessageBox.Show("O Form2 já está aberto!");
}
else
{
    Form2 form = new Form2();
    form.Show();
}
  • In the case Form2 would be the name of the form.

1

Check instance. If it exists, bring forward and maximize.

if (Application.OpenForms.OfType<Form1>().Count() > 0)
{
      Application.OpenForms["Form1"].BringToFront();
      Application.OpenForms["Form1"].WindowState = FormWindowState.Maximized;
}
else
{
      Form1 frmForm1 = new Form1();
      frmForm1.Show();
}

-2

I’d do it this way: var frmInicio = new frmInicio(); frmInicio.Showdialog();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.