Layout MDI Child forms

Posted on 2009-04-13
Last Modified: 2012-05-06
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?
Question by:Sheritlw
LVL 10

Expert Comment

ID: 24129114
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

LVL 48

Expert Comment

ID: 24129652
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


Author Comment

ID: 24132120
The procedures listed for getting the forms are great, but how do I let them Tile Veritically/Horizontally just those two forms?
LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 24309039
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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now