Question : MDI Child Layout Issues

I've posted this in MSDN forums with no response.  I'm hoping someone here can help me resolve...

I'm developing in VB .NET Visual Studio 2005.  I have an MDI form.  I want to be able to arrange child forms in the MDI in one of 4 ways:  Horizontally tiled, Vertically tiled, Minimized, Maximized.  I can tile the children with no problem using Me.LayoutMdi(MdiLayout.TileVertical) and Me.LayoutMdi(MdiLayout.TileHorizontal).  I can minimize all windows ok by looping thru the mdi children and setting their window state to minimized.

My problem is getting all children maximized after I have tiled them...  If I start the app up and don't call LayoutMdi, my children come up maximized and things work ok in that respect.  But, if I tile the windows and then later decide I want them all maximized, it doesn't work.  It will maximize the topmost child and when I close that child, the other children show as either cascaded or minimized, even tho I have looped thru all of them and set their window state to maximized.  I have worked with this a bit and I found the scenario that causes this issue...

I create a new solution with an mdi and a child form.  My MDI code is pasted at the bottom of this post.

I didn't modify the default MDI form very much except to remove the buttons and menus that I didn't need and to add a new menu item (entitled "Maximize All" and named "MaxmizeAllToolStripMenuItem") to the "Windows" menu that maximizes all the child forms.

My solution also has a child form called Form1.  There's nothing on Form1...its just a form.

Now, if I run the solution, I can click on the "NewToolStripButton" and create multiple child instances of Form1.  They display maximized in the MDI container.  That is the correct behavior.  If I click on "Tile Horizontal", all of the child forms tile horizontally correct.  If I click on "Maximize All", all of the child forms are maximized with only one child showing in the MDI container (others are behind it in a  maximized state).  If I click on the "X" to close each child, the current child will be closed and the next child is visible in a maximized state as it should be.  I keep clicking "X" to close all my child forms, and again, as I close one, the next is displayed in a maximized state as it should be.  

Now, if I modify Form1 and add a DataGridView to it, the behavior changes, depending on how I set the Anchor and Dock properties on the DataGridView.

If I set DataGridView Anchor = "Top, Left" and Dock = "None" and go thru the same test steps above, after I click on "Maximize All", it appears to be working as it should with the children maximized, however, what really happened is that only one child was maximized and the others are still tiled horizontally behind it.  When I click on the "X" to close the top maximized child, all of the other children are still tiled.

Now, If I set DataGridView Anchor = "Top, Bottom, Left, Right" and Dock = "None" OR Anchor = "Top, Left" and Dock = "Fill" and go thru the same test steps, the results are as they should be...  The child forms work properly as they did when Form1 had no controls on it.

Can you explain what I need to do to fix this problem?  I cannot always have my datagridview with either Anchor = "Top, Bottom, Left, Right" or Dock = "Fill".

Thanks for any help you can provide!

MDI Code:
Private m_ChildFormNumber As Integer

Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
' Create a new instance of the child form.
Dim ChildForm As New Form1
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = Me
ChildForm.WindowState = FormWindowState.Maximized
m_ChildFormNumber += 1
ChildForm.Text = "Window " & m_ChildFormNumber
ChildForm.Show()
End Sub

Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CascadeToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.Cascade)
End Sub

Private Sub TileVerticleToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileVerticalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub

Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileHorizontalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub

Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ArrangeIconsToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.ArrangeIcons)
End Sub

Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CloseAllToolStripMenuItem.Click
' Close all child forms of the parent.
For Each ChildForm As Form In Me.MdiChildren
ChildForm.Close()
Next
End Sub

Private Sub MaxmizeAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaxmizeAllToolStripMenuItem.Click
For Each ChildForm As Form In Me.MdiChildren
ChildForm.WindowState = FormWindowState.Maximized
Next
End Sub

Answer : MDI Child Layout Issues

Hi,
I tried by creating a solution in visual studio 2008. I created an mdi container form and used a menu item to create multiple mdi children for this form. The mdi child form has a normal datagridview on it. by normal i mean its anchor set to top, left and the dock to none. I could not replicate your problem.

A work around could be to use a variable to hold the currently selected window state and then on activated or gotfocus event of each form, check to see if the variable holds the value to maximize then change the window state of current form to maximized and refresh the form.

Hope it helps.

Code Cruiser
Random Solutions  
 
programming4us programming4us