How to split the screen with 2 Open Forms within an MDI

Asked

Viewed 200 times

2

Good morning to you all. I have a question about forms. In my application, I can open 2 forms with grids where they present factory information. Those Grids are updated every 30 seconds, and are opened with:

StartPosition  --> WindowsDefaultLocation.
WindowState --> Normal
Size --> 1002;551

Doubt: I can boot them by making the Form1 open maximized, but on the restore button I can resize the Size of it to half the monitor screen? And form2 can identify if there is already an open form and resize Form2 to the second half of the screen with shows the example below.

inserir a descrição da imagem aqui

It follows the code I’m working on, but incomplete, I’m testing the possibilities so it’s not functional, just a notion. I thought I’d use the event SizeChanged for that application.

    int lx, ly;
    int sw, sh;
    private void frm_Visualizar_Grid_SizeChanged(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {

            lx = this.Location.X;
            ly = this.Location.Y;
            sh = this.Size.Height;

            this.Size = Screen.PrimaryScreen.WorkingArea.Size;
            this.Location = Screen.PrimaryScreen.WorkingArea.Location;
        }
        frm_principal f = new frm_principal();
        if (this.WindowState == FormWindowState.Maximized)
        {                  
            //string cont = Application.OpenForms.Count.ToString();
            string cont = f.MdiChildren.Length.ToString();

            if (Convert.ToInt16(cont) < 2)
            {
                //sw = f.MdiParent.Size.Width;

                this.Size = new Size((sw / 2), sh);
                this.Location = new Point(lx,ly);
            }
            else
            {
                sw = f.MdiParent.Size.Width;
                this.Size = new Size((sw / 2), sh);
                //if (this.Location.IsEmpty)
                f.StartPosition = FormStartPosition.Manual;

            }
        }
    }

Thank you all.


I tried to apply the tip that our friend João Martins mentioned in the answer but I did not succeed.

    int lx, ly;
    int sw, sh;
    private void frm_Visualizar_Grid_SizeChanged(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {

            this.Size = Screen.PrimaryScreen.WorkingArea.Size;
            this.Location = Screen.PrimaryScreen.WorkingArea.Location;
        }
        if (this.WindowState == FormWindowState.Maximized)
        {
            //foreach (frm_Visualizar_Grid frm in MdiChildren)
            foreach (Form frm in Application.OpenForms)
            {
                this.LayoutMdi(System.Windows.Forms.MdiLayout.TileVertical);
            }
        }
    }

1 answer

0

Try evoking the following method:

this.LayoutMdi(System.Windows.Forms.MdiLayout.TileVertical);

Will make all the MdiChild organize themselves vertically.

  • Thanks for the help John, I tried to apply your tip but I still did not succeed. I edited my question with more information.

  • @William, the code I put in is supposed to be used when all the Foms are resized. It is a method that should be invoked only once, with the click of a button, for example.

  • I understood, but I think it does not apply in this case because the intention is not to resize with the mouse or have a button for it. Just resize the form to half the screen by clicking restore.Would you have any example of this application? I will make a new project and try to exemplify this method.

  • Note that the method LayoutMdi must be applied to MdiParent, that is, to the form containing the children.

Browser other questions tagged

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