Do not open more than one form at the same time in C#

Asked

Viewed 2,496 times

2

I’m developing a system that’s practically a CRUD.

My problem and the following: when I open a screen of my system, if I click on the menu to open again, it has two windows open, that is, if I click several times in the menu will be with several windows open.

I would like to know how I make the system not allow to open more than one window on the system at the same time? Only allow to open one at a time.

my main form containing the menu is the parent of the other forms.

and the other forms open inside Mdiparent

  • How are Forms opened? Inside a Mdiparent?

  • Edit the question to inform you that it uses parent form.

  • See this solution may be of the same breed as your! :)

4 answers

2


From what I found searching, it is not possible to open a form mdi son as dialog.

You have two options:

  1. Do not "set" the child form as mdi child and treat it as a normal form. So you can open with .ShowDialog();
  2. Use this mini framework to open the child form with dialog alternatively;

0

Hermano cool, open the form that way:

DialogResult dialogResult = frmNomeDoSeuForm.ShowDialog();

But I have a tip, as today the needs of multiscreen systems would be interesting you just check if that same form is already open... if yes leave it in the foreground and focus.... so you can leave on another screen another part of the system... see is simple...

        if (Application.OpenForms.OfType<FrmNomeDoForm>().Count() > 0)
        {
            Application.OpenForms.OfType<FrmNomeDoForm>().First().Focus();
        }
        else
        {
            FrmNomeDoForm frmNomeDoForm = new FrmNomeDoForm();
            frmNomeDoForm.Show();
        }
  • ok! we are here for this! hug friends! hope you can help!

  • I would still remove the factual language used, but I believe I’m actually answering the question now

0

Using Singleton:

public partial class Form2 : Form
{
 .....
 private static Form2 inst;
 public static Form2  GetForm
 {
   get
    {
     if (inst == null || inst.IsDisposed)
         inst = new Form2();
     return inst;
     }
 }
 ....
}
Invoke/Show this form,

Form2.GetForm.Show();

Source: https://stackoverflow.com/questions/1403600/how-to-avoid-multiple-instances-of-windows-form-in-c-sharp

I found this solution on the net:

frm2 f = Application.OpenForms["NameOfForm2"];
if(f != null)
   f.BringToFront();
else
{
   frm2 f = new frm2();
   f.Show();
}

Source: https://stackoverflow.com/questions/21080083/how-to-prevent-my-program-to-open-the-same-window-form-twice

Or see in the example below where I check if outlook is running before starting it. You can do a check of this type on your screen builder.

//Se não estiver iniciado, inicia uma instância do outlook
if (Process.GetProcessesByName("OUTLOOK").Count().Equals(0))
{
    Process.Start("outlook.exe");//Aqui continue seu código normalmente
}
else
{
   //Cancele o processo
   Window.GetWindow(this).Close();
}

0

He also had this doubt. I followed the tip of Tiago de Abreu and it worked. Follows final code:

if (Application.OpenForms.OfType<frmCad_Categoria>().Count() > 0)
{
    Application.OpenForms.OfType<frmCad_Categoria>().First().Focus();
}
else
{
    frmCad_Categoria form = new frmCad_Categoria();
    form.MdiParent = this;
    form.Show();
}

Browser other questions tagged

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