Resizing a MDI Form while MDIChildren forms exist?

I have to be able to resize my Windows Application main form. It is a MDI Parent form. As an example, there are 3 MDI Child forms open. If the user resizes the Parent form, it cannot go smaller than the largest MDI Child form +20 pixels added for height and width. Does anyone have an example of such code. This is a Windows VB.Net application.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
There is a MinimumSize property on forms. Simply make sure that this property is set to the largest child's Size.

This means that everytime that you add or remove a Child, you should look through the existing childs, find the largest one and then set that value, + the 20 pixels, to the MinimumSize property of the MDI parent.
BlakeMcKennaAuthor Commented:

Is your solution assuming the BorderStyle of the MDI Parent is "Sizable"?
Jacques Bourgeois (James Burger)PresidentCommented:
Yes, because you stated "If the user resizes the Parent form". It needs to be Sizable for the user to resize it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

BlakeMcKennaAuthor Commented:
Thanks James...that worked well!
BlakeMcKennaAuthor Commented:

I jumped to soon. the code that is suppose to allow a user to resize the MDI Parent form based on the largest open/active form within the MDI Parent.

    Public Sub GetMaxFormSize()
            Dim iHeight As Integer = 0
            Dim iWidth As Integer = 0

            For Each frm As Form In Application.OpenForms
                If frm.Width > iWidth Then
                    iWidth = frm.Width
                End If

                If frm.Height > iHeight Then
                    iHeight = frm.Height
                End If

            stcFormSize.Height = iHeight
            stcFormSize.Width = iWidth

        Catch ex As Exception
            EH.ErrorMessage = "modMain/GetMaxFormSize() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window

The above SubProcedure gets called in the "FormClosed" event of each MDI Child form. Below is the resizing logic for my MDI Parent form.

    Private Sub frmMain_ResizeBegin(sender As Object, e As EventArgs) Handles Me.ResizeBegin
    End Sub
    Private Sub frmMain_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
            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.MinimumSize = New Size(Me.Width, Me.Height)
                ElseIf blnGreatHeight Then
                    Me.MinimumSize = New Size(stcFormSize.Width + nonClientWidth + 10, Me.Height)
                ElseIf blnGreatWidth Then
                    Me.MinimumSize = New Size(Me.Width, stcFormSize.Height + nonClientHeight + MenuStrip1.Height + Me.sbr.Height + ts1.Height + 10)
                    Me.MinimumSize = 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

Open in new window

Here is the scenario that makes me know this code isn't working:

I have two open MDI Child forms. Form 1 is quite larger than the Form 2. I try and resize the MDI Parent to be smaller than the Form 1 and it does what it is suppose to. However, when I close Form 1 and then try to resize again based on the smaller Form 2, the MDI Parent still uses the width/height values from Form 1.

Am I using the right collection in the "Application" object; "OpenForms"?

By the way you know if it's possible for a user such as myself to reopen a closed thread?
Jacques Bourgeois (James Burger)PresidentCommented:
I do not have the time to test, and anyway, my tests would be wordless because I do not have the whole picture.

Here are a few things to look for.

First thing is to try with your MDI form MDIChildren property instead of Application.OpenForms. OpenForms considers all the forms opened in the application, while MDIChildren looks only for the childs of the MDI. You are thus looking only for what you need to check in your situation. This could also prevents some of the following problems because of the sequence in which things happen (for instance, the child is released from the MDI before it is released from memory).

If this does not work:

Have you put a breakpoint in your resizing routine to make sure that it is called when you thing it is. Maybe it is simply not called after closing Form1.

Do you create you child forms by declaring variables and instantiating them yourself (calling New), or do you simply call the form or use My.Forms? When you simply call the form (à la VB6) or use My.Forms, you lose control. You do not know really what happens in the background. The system simply creates global variables, and it is possible that these are not released properly when you call Close.

You might try to call Application.DoEvents just after you close Form1. This forces the system to terminate all the events before going on. It might be possible that some event in Form1 delays its complete closing, and that the form is still in memory when you run your resizing routine.

Are you sure that your instance of Form1 has been released properly. Inserting a breakpoint where you loop through Application.OpenForms and looking what hangs in OpenForms with the debugger might show that Form1 is still in memory.

A thread cannot close... because it does not open. What you ask is the same as asking if you can open a method that is closed.

A thread is started, it runs, and stops when it has finished execution.

The same way that you can recall a method that has finished executed, you can restart a new thread using the same variable, but that is all. Except for variable, the new thread is completely independent from the first one. And naturally, that implies that you have access to the Thread variable.
BlakeMcKennaAuthor Commented:

I figured it out. The solution was a lot simpler than I had originally coded.

Thanks for the answer to the Thread question to.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.