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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now