Home > Net >  My forms in the mdi parent are not docking
My forms in the mdi parent are not docking

Time:11-10

This is the code in the mdi parent form.

            this.IsMdiContainer = true;
            this.LayoutMdi(MdiLayout.TileHorizontal);
            sideControlPanelForm.MdiParent = this;
            userForm.MdiParent = this;

This is the sidecontrolpanelforms initilization

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Size = new Size(400, int.MaxValue);
            this.Dock = DockStyle.Left;
            this.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;

this is the userform initilization

            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Dock = DockStyle.Left;
            this.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;

The two forms right now are overlapping and nothing else. But i want them to dock with eachother side by side. Individually the forms are working as i intented for them to work with anchoring and stuff. Would i add a other component like a splitter then the forms will dock to that, just not eachother.

Edit... I have not tried it yet, but should i just keep track of one forms width and adjust the others all the time? This seems like a lot of work for something that docking should handle. But if its the only way i can do it.

CodePudding user response:

I solved it, it looks pretty good if i must say it myself.

        Size sidePanelSize = new Size(0,int.MaxValue);
        Size contentPanelSize = new Size(0, int.MaxValue);
        const int widthDivider = 5;

        private void form1SetSize()
        {
            form1Form.Size = sidePanelSize;
        }
        private void form2SetSize()
        {
            form2Form.Size = contentPanelSize;
        }

        private void formSizeChanged(object sender, EventArgs e)
        {
            Debug.WriteLine("SHIT HAS CHANGED");
            calculateFontSize();
            //calculate new width
            form1SetSize();
            sideControlSetSize();

        }
        private void calculateFontSize()
        {
            //calcualte width
            sidePanelSize.Width =  this.Size.Width / widthDivider;
            contentPanelSize.Width = this.Size.Width - sidePanelSize.Width;
            setContentFormXLocation();
        }
        private void setContentFormXLocation()
        {
            form2Form.Left = form1.Bounds.Left   sidePanelSize.Width;
        }

This is all the code, there is no flickering and stuff. I think it looks very good when resizing the window. Now adding another window might get complicated but i dont need it so im happy.

If anyone has a better solution please feel free to answer.

  • Related