Solved

Layout MDI Child forms

Posted on 2009-04-13
5
773 Views
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?
Thanks
0
Comment
Question by:Sheritlw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 10

Expert Comment

by:MrClyfar
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.
Jas.

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
        Next
    End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
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)
            Else
                Dim total As Integer = hash.Item(frm.Name)
                hash.Item(frm.Name) = total + 1
            End If
 
        Next
 
 
        ' Shows the results
        For Each item As DictionaryEntry In hash
            If item.Value > 1 Then
                Debug.WriteLine(item.Key.ToString)
            End If
        Next

Open in new window

0
 

Author Comment

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

Accepted Solution

by:
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!

Example:
Public Class mdiParent
 
    Private Enum TileDirection
        Vertical
        Horizontal
    End Enum
 
    Private Sub TileVertical_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileVertical.Click
        TileForms(TileDirection.Vertical)
    End Sub
 
    Private Sub TileHorizontal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileHorizontal.Click
        TileForms(TileDirection.Horizontal)
    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
                frm.BringToFront()
            Next
        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
                    matches.Add(frm)
                End If
            Next
        End If
        Return matches
    End Function
 
End Class

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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