anchoring and docking MDI child forms

AndyNakamura 51 Reputation points
2021-02-01T12:53:41.957+00:00

I have am MDI parent form (FrmMain) that displays an mdi child (FrmSearch) on loading.
The parent form has a menustrip at the top
the child form dockstyle is set to fill
The child form displays correctly in the parent form on loading
If I display another child form (FrmTwo) by hiding FrmSearch and opening FrmTwo then
on closing FrmTwo and showing FrmSearch it does not correctly fill the Parent.
If I resize the parent with the mouse the child form immediately aligns itself correctly.
I've tried
frmSearch.dock = dockstye.fill
FrmSearch.show

from FrmTwo

Anyone know how to get the FrmSearch to correctly fill the Parent correctly on the form being shown without having to manually adjust the main forms size?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,572 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Michael Taylor 48,306 Reputation points
    2021-02-01T15:45:33.063+00:00

    Setting DockStyle seems wrong to me. An MDI child is not the same thing as a child control. To parent the child you would normally set MdiParent on the child. To make the child take up the entire parent you should just set the form to maximized and it should maximize in the parent. Once that is done you shouldn't have to do anything else. To cover up the current child with another child then do the same thing. Of course the user could change the min/max state and rearrange as needed.

    To get notified when the MDI child is activated or closed then use the MdiChildActivate event which can allow you to adjust other children.

    1 person found this answer helpful.
    0 comments No comments

  2. Michael Taylor 48,306 Reputation points
    2021-02-01T16:41:56.933+00:00

    Sounds like your control ordering is wrong if you're having an issue with the menu strip. The designer will insert controls into the form in the order you added them to the designer. In some cases if the ordering is wrong then you get issues with docking overwriting other controls. It sort of sounds like your menu strip was perhaps added after the other controls which causes it to overlay them incorrectly. Adjusting the order in the designer.cs should resolve that.

    Example.

    Public Class Form1
        Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
            _count += 1
            Dim id = _count
            Dim title = $"MDI {id}"
    
            Dim form = New Form()
            form.BackColor = Color.Yellow
            form.Text = title
            form.MdiParent = Me
            form.WindowState = FormWindowState.Maximized
    
            Dim label = New Label
            label.Text = title
            form.Controls.Add(label)
    
            form.Show()
        End Sub
    
        Private Shared _count = 0
    
        ' Auto generated by VS
       Private Sub InitializeComponent()
            Me.MenuStrip1 = New System.Windows.Forms.MenuStrip()
            Me.NewToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
            Me.MenuStrip1.SuspendLayout()
            Me.SuspendLayout()
            '
            'MenuStrip1
            '
            Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.NewToolStripMenuItem})
            Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
            Me.MenuStrip1.Name = "MenuStrip1"
            Me.MenuStrip1.Size = New System.Drawing.Size(800, 24)
            Me.MenuStrip1.TabIndex = 1
            Me.MenuStrip1.Text = "MenuStrip1"
            '
            'NewToolStripMenuItem
            '
            Me.NewToolStripMenuItem.Name = "NewToolStripMenuItem"
            Me.NewToolStripMenuItem.Size = New System.Drawing.Size(43, 20)
            Me.NewToolStripMenuItem.Text = "&New"
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(800, 450)
            Me.Controls.Add(Me.MenuStrip1)
            Me.IsMdiContainer = True
            Me.MainMenuStrip = Me.MenuStrip1
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.MenuStrip1.ResumeLayout(False)
            Me.MenuStrip1.PerformLayout()
            Me.ResumeLayout(False)
            Me.PerformLayout()
        End Sub
    End Class
    

    Each child window is maximized in the parent. The menu does not overlay them as there is a label at the 0,0 position and it is shown properly. Since the children are maximized closing a child window will have the previous child maximized as well.

    1 person found this answer helpful.
    0 comments No comments

  3. AndyNakamura 51 Reputation points
    2021-02-01T16:29:28.443+00:00

    Hi,
    That's how I tried to do it originally.
    The problem is if I don't use the dockstyle when the form loads, part of the form ends up behind the menustrip.
    I've made a simple app with some coloured panels to try and get a handle on it.
    There's an mdi parent form and a couple of child forms.
    up to now the only way I can get the child forms to align properly is by have a sub in the main form and programatically increasing the main form size by 1 pixel and then reducing it by 1 pixel. Then everything aligns correctly.
    Obviously not good

    0 comments No comments

  4. AndyNakamura 51 Reputation points
    2021-02-01T17:32:31.317+00:00

    Hi,
    Not sure what you mean by control ordering.
    The only control on the parent form is the menustrip.
    I inserted that by dragging from the toolbox in design view.


  5. AndyNakamura 51 Reputation points
    2021-02-01T18:46:45.71+00:00

    Hi,
    I'm using vb.net
    I guess I could start a C# project and try, but I'm completely unfamiliar with C#
    Bit short of time at the moment though.