Layout MDI Child forms

Hi again,

Users are allowed to open multiple instances of a form.   Sometimes, although they may have other forms open, they will need to see just the two instances of one form side by side or up/down.
I would like to create a list that would display all forms that have multiple instances running and give them the option to layout just the selected form instances.
Any ideas?
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Here is a completely generic solution...

When the ToolStrip buttons are clicked (TileVertical, TileHorizontal), the ACTIVE MdiChild will be used to find all children of that same Type.  They will then be tiled evenly vertically or horizontally based on the button clicked.  So it would work with 2, 3, 4, or however many of that type you currently have open!

Public Class mdiParent
    Private Enum TileDirection
    End Enum
    Private Sub TileVertical_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileVertical.Click
    End Sub
    Private Sub TileHorizontal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileHorizontal.Click
    End Sub
    Private Sub TileForms(ByVal direction As TileDirection)
        Dim frms As List(Of Form) = FindSimilarForms(Me.ActiveMdiChild)
        If frms.Count > 1 Then
            Dim mc As MdiClient = Me.Controls.OfType(Of MdiClient).First
            Dim sz As Integer = IIf(direction = TileDirection.Vertical, Math.Floor(mc.ClientSize.Width / frms.Count), Math.Floor(mc.ClientSize.Height / frms.Count))
            For i As Integer = 0 To frms.Count - 1
                Dim frm As Form = frms(i)
                If frm.WindowState <> FormWindowState.Normal Then
                    frm.WindowState = FormWindowState.Normal
                End If
                Select Case direction
                    Case TileDirection.Vertical
                        frm.SetBounds(i * sz, 0, sz, mc.ClientSize.Height)
                    Case TileDirection.Horizontal
                        frm.SetBounds(0, i * sz, mc.ClientSize.Width, sz)
                End Select
        End If
    End Sub
    Private Function FindSimilarForms(ByVal child As Form) As List(Of Form)
        Dim matches As New List(Of Form)
        If Not IsNothing(child) Then
            For Each frm As Form In Me.MdiChildren
                If frm.GetType.Equals(child.GetType) Then
                End If
        End If
        Return matches
    End Function
End Class

Open in new window

Jason EvansSenior Software DeveloperCommented:
Hi there.
I've written a quick and dirty routine that iterates through all the child forms of an MDI parent, keeping count of the number of time each form appears. So in my example, I had two child forms in the project - frm1 and frm2. Whilst running the code, I created 2 instances of frm1 and one instance of frm2.
When I ran GetFormCount() it found that there were two instances of frm1 and one of frm2.
Hope this helps.

Private Sub GetFomCount()
        Dim formcount1 As Integer
        Dim formcount2 As Integer
        For Each frm As Form In MdiChildren
            If TypeOf frm Is frm1 Then
                formcount1 += 1
            End If
            If TypeOf frm Is frm2 Then
                formcount2 += 1
            End If
    End Sub

Open in new window

Jorge PaulinoIT Pro/DeveloperCommented:
It's like MrClyfar solution but a little more flexible. If you don't know how many forms you will have you don't know how many variables you will need.
I suggest you to use an HashTable and use the Keys for the form names and the Values for the total instances open. Something like this:

        Dim hash As New Hashtable
        ' Loops in all form childs
        For Each frm As Form In MdiChildren
            ' If the frm is not already in the table
            If Not hash.ContainsKey(frm.Name) Then
                hash.Add(frm.Name, 1)
                Dim total As Integer = hash.Item(frm.Name)
                hash.Item(frm.Name) = total + 1
            End If
        ' Shows the results
        For Each item As DictionaryEntry In hash
            If item.Value > 1 Then
            End If

Open in new window

SheritlwAuthor Commented:
The procedures listed for getting the forms are great, but how do I let them Tile Veritically/Horizontally just those two forms?
All Courses

From novice to tech pro — start learning today.