How to resize an MDIChild to fit exactly the MDIParent forms client region?

Posted on 2009-04-21
Last Modified: 2012-05-06
I am trying to size MDIChild forms within the MDIParent forms client region.  I have found code that is supposed to work but does exactly what it says it prevents, so it's useless.  I am trying this code:
(  Me.Size = MdiParent.ClientSize - MdiParent.DefaultMargin.Size  ) but I am still getting the scroll bars on my MDIChild form.  How do I eliminate the MDIChild form scroll bars?
Question by:ljmilano
    LVL 15

    Expert Comment

    I may be stating the obvious, but why not maximise the child form.

    You will get scroll bars on the child form if there are controls outside the visible area. You can use the Dock and or Anchor properties of the controls to ensure that controls always fit where you want them to in a form no matter how it is resized

    Author Comment

    Hi ChloesDad, thanks, but that's not really what I'm looking for.  I've seen this comment all over the internet, and I'm sure you have... "it used to be so easy in VB6".  I like the look and feel of sizing it without maximizing it, and now, I really just want to know how to do it because I can not figure it out.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    This worked for me:
    Public Class Form1
        ' ...this is the MdiParent form...
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each ctl As Control In Me.Controls
                If TypeOf ctl Is MdiClient Then
                    Dim f2 As New Form2
                    f2.StartPosition = FormStartPosition.Manual
                    f2.Size = ctl.ClientSize
                    f2.Location = New Point(0, 0)
                    f2.MdiParent = Me
                End If
        End Sub
    End Class

    Open in new window

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    This is what it looked like:

    Author Comment

    Hi're not going to believe this, but I get the same's my code, I still get the scroll bars.
            ' Create a new instance of the child form.
            Dim ChildForm As New frmImport

            ChildForm.StartPosition = FormStartPosition.Manual
            ChildForm.Size = Me.ClientSize
            ChildForm.Location = New Point(0, 0)
            ChildForm.MdiParent = Me
    More help is greatly appreciated...thanks..

    LVL 85

    Accepted Solution

    That is NOT the same code that I used...  =\

    Note that I used a LOOP to iterate the controls collection of the MdiParent to find the "MdiClient" instance.  Then I set the size of the child form to the size of THAT control...and not the Form itself which is what you did with "Me.ClientSize".

    Look again CLOSELY at my code please!  =)

    Author Comment

    Hi Idle Mind:  Got It!!!!
    Couple of points.  I actually put that code in a procedure that loads the frmImport from a menu option on the MDIParent form.  So, I figured I already had the MDIChild instance and wouldn't have to search through a loop for it...which means I didn't read your code correctly, I needed the MDIClient.  Sorry, I was looking at it after a long day.  Thanks for your help.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Glad it was helpful!  The MdiClient class is one of those things you would be unlikely to find on your own just reading the help in the documentation:

    I picked it up myself a long time ago here on EE...  =)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    758 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