Results 1 to 7 of 7

Thread: [RESOLVED] Checking for MDIChildren Forms?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Checking for MDIChildren Forms?

    I have a routine that checks to see what MDIChildren forms are open and then retrieve the largest forms width/height values. It's not working like it's suppose to. This routine is called from the "FormClosed" Event of each MDIChild form. Here it is below.

    Code:
        Public Sub GetMaxFormSize()
            Try
                Dim iHeight As Integer = 0
                Dim iWidth As Integer = 0
    
                For x = 0 To (frmMain.MdiChildren.Length) - 1
                    Dim tempChild As Form = CType(frmMain.MdiChildren(x), Form)
    
                    If tempChild.Width > iWidth Then
                        iWidth = tempChild.Width
                    End If
    
                    If tempChild.Height > iHeight Then
                        iHeight = tempChild.Height
                    End If
                Next x
    
                stcFormSize.Height = iHeight
                stcFormSize.Width = iWidth
    
            Catch ex As Exception
                EH.ErrorMessage = "modMain/GetMaxFormSize() - " & ex.Message & "...Contact Engineering!" & "~E"
            End Try
        End Sub
    The way this code is suppose to work is that whenever a MDIChild form closes, it executes the above routine. It simply loops thru all the MDIChild forms that are open and assigns the highest values of the width/height properties of the MDIChild form. The "stcFormSize" is a structure and is referenced in the "ResizeEnd" Event of the MDIParent form. Below is the "ResizeEnd" Event code:

    Code:
        Private Sub frmMain_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
            Try
                Dim blnGreatHeight As Boolean = False
                Dim blnGreatWidth As Boolean = False
    
                Dim cnt As Integer = Me.MdiChildren.Length
    
                If cnt >= 1 Then
                    Dim nonClientWidth As Integer = Me.Size.Width - Me.ClientSize.Width
                    Dim nonClientHeight As Integer = Me.Size.Height - Me.ClientSize.Height
    
                    If Me.Width > (stcFormSize.Width + nonClientWidth + 10) Then
                        blnGreatWidth = True
                    End If
    
                    If Me.Height > (stcFormSize.Height + nonClientHeight + MenuStrip1.Height + Me.sbr.Height + ts1.Height + 10) Then
                        blnGreatHeight = True
                    End If
    
                    If blnGreatHeight And blnGreatWidth Then
                        Me.Size = New Size(Me.Width, Me.Height)
                    ElseIf blnGreatHeight Then
                        Me.Size = New Size(stcFormSize.Width + nonClientWidth + 10, Me.Height)
                    ElseIf blnGreatWidth Then
                        Me.Size = New Size(Me.Width, stcFormSize.Height + nonClientHeight + MenuStrip1.Height + Me.sbr.Height + ts1.Height + 10)
                    Else
                        Me.Size = New Size(stcFormSize.Width + nonClientWidth + 10, stcFormSize.Height + nonClientHeight + MenuStrip1.Height + Me.sbr.Height + ts1.Height + 10)
                    End If
                End If
    
            Catch ex As Exception
                EH.ErrorMessage = "frmMain/frmMain_Load() - " & ex.Message & "~E"
            End Try
    
            EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
        End Sub
    If anyone has any suggestions for making this code more efficient, I'm all EARS!!!!

    Thanks,
    Blake

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Checking for MDIChildren Forms?

    I cannot execute your code because there are some calls are missing.

    What i understood is that, you need the frmMain's width/height always bigger than any child's width/height, if so then try this code with new project

    I think there is no need to call GetMaxFormSize in each MDIChild form closes, just call it whenever the MDI form resized.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Form2.MdiParent = Me
            Form2.Size = New Size(600, 500)
            Form2.Show()
    
            Form3.MdiParent = Me
            Form3.Size = New Size(400, 100)
            Form3.Show()
    
            Form4.MdiParent = Me
            Form4.Size = New Size(100, 200)
            Form4.Show()
        End Sub
    
        Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
    
            If Me.MdiChildren.Length >= 1 Then
    
                Dim blnGreatHeight As Boolean = False
                Dim blnGreatWidth As Boolean = False
                Dim stcMaxSize As Size = GetMaxFormSize()
    
                If Me.Width > stcMaxSize.Width Then
                    blnGreatWidth = True
                End If
    
                If Me.Height > stcMaxSize.Height Then
                    blnGreatHeight = True
                End If
    
                If blnGreatHeight And blnGreatWidth Then
                    ' No need for resizing.
                ElseIf blnGreatHeight Then
                    Me.Size = New Size(stcMaxSize.Width, Me.Height)
                ElseIf blnGreatWidth Then
                    Me.Size = New Size(Me.Width, stcMaxSize.Height)
                Else
                    Me.Size = New Size(stcMaxSize.Width, stcMaxSize.Height)
                End If
            End If
    
        End Sub
    
        Private Function GetMaxFormSize() As Size
    
            Dim iHeight As Integer = 0
            Dim iWidth As Integer = 0
    
            For x = 0 To (Me.MdiChildren.Length) - 1
                Dim tempChild As Form = CType(Me.MdiChildren(x), Form)
    
                If tempChild.Width > iWidth Then
                    iWidth = tempChild.Width
                End If
    
                If tempChild.Height > iHeight Then
                    iHeight = tempChild.Height
                End If
            Next x
    
            Return New Size(iWidth, iHeight)
    
        End Function
    
    End Class



  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Checking for MDIChildren Forms?

    While I can't test it, you could probably shorten the GetMaxFormSize in 4x2y's example to:
    Code:
    Private Function GetMaximumSize(ByVal parent As Form) As Size
    	Dim width As Integer = parent.MdiChildren.Max(Function (m) m.Size.Width)
    	Dim height As Integer = parent.MdiChildren.Max(Function (m) m.Size.Height)
    
    	Return New Size(width, height)
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Checking for MDIChildren Forms?

    Quote Originally Posted by dday9 View Post
    While I can't test it, you could probably shorten the GetMaxFormSize in 4x2y's example to:
    Yes, it works!

    more shortening
    Code:
        Private Function GetMaxFormSize() As Size
            Return New Size(Me.MdiChildren.Max(Function(m) m.Size.Width), Me.MdiChildren.Max(Function(m) m.Size.Height))
        End Function



  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Checking for MDIChildren Forms?

    You both are returning max width and height. These could be from Two separate forms which wont indicate the largest form. Unless i have missread and these are Two sizes the OP wants for One size to be combined.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Checking for MDIChildren Forms?

    ident,

    It works....I tested it out with multiple MDI Child forms opened.

    Thank you all for your input!
    Blake

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Checking for MDIChildren Forms?

    Quote Originally Posted by blakemckenna View Post
    It works....I tested it out with multiple MDI Child forms opened.
    Good



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width