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

2

I am developing a system that is practically a CRUD.

My problem is this: when I open a screen of my system, if I click the menu to open again, you have two windows open, ie if I click several times in the menu you will have several windows open.

I'd like to know how do I make the system not allow me 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 form.

and the other form opens within MDIParent

    
asked by anonymous 24.02.2017 / 20:32

3 answers

2

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

You have two options:

  • Do not "set" the child form as mdi child and treat it as a normal form. This way you can open with .ShowDialog() ;
  • Use this mini framework to do the form child to open with dialog alternatively;
  • 24.02.2017 / 23:49
    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: link

    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: link

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

    //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();
    }
    
        
    24.02.2017 / 21:17
    0

    Hermano boa, open the form this way:

    DialogResult dialogResult = frmNomeDoSeuForm.ShowDialog();
    

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

            if (Application.OpenForms.OfType<FrmNomeDoForm>().Count() > 0)
            {
                Application.OpenForms.OfType<FrmNomeDoForm>().First().Focus();
            }
            else
            {
                FrmNomeDoForm frmNomeDoForm = new FrmNomeDoForm();
                frmNomeDoForm.Show();
            }
    
        
    27.06.2017 / 15:56