Results 1 to 4 of 4

Thread: [RESOLVED] Usercontrol on MDI Child cant get width/height of its parent

  1. #1

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Resolved [RESOLVED] Usercontrol on MDI Child cant get width/height of its parent

    Hello!

    A usercontrol placed on a form will be initialized before the acutal form. Thats not a problem as long as the form is not an mdi child, so the usercontrol can get the width and height properties of the form. But as long as i set the form to be an mdi child, the Width and the Height properties will be 0.

    The problem is, i have done a usercontrol that will resize itself, to completely hide the actual form.

    It works on plain forms, but i cant get it to work on MDI childs. It also hopeless to resize the mdi child at load, because it will be initialized after the usercontrol.

    Is there any workaround?
    Attached Files Attached Files

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Usercontrol on MDI Child cant get width/height of its parent

    Would this be of any help?

    Code:
    Option Explicit
    Dim WithEvents ParentForm As Form
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
       On Error Resume Next  ' In case parent isn't a form.
       Set ParentForm = Parent
    End Sub
    
    Private Sub ParentForm_Resize() ' This event fires when the parent form resizes.
       Debug.Print Parent.ScaleWidth & "," & Parent.ScaleHeight
    End Sub

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Usercontrol on MDI Child cant get width/height of its parent

    You can expand Edgemeal's sample to skip On Error, because you can check the type before trying to assign:
    Code:
    If TypeOf Parent Is Form Then
        Set ParentForm = Parent
    ElseIf TypeOf Parent Is MDIForm Then
        Set ParentMDI = Parent
    Else
        ' this shouldn't happen in VB, non-VB app
    End If

    You can also consider the following:
    Code:
    Option Explicit
    
    Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As Long) As Long
    
    Private Sub UserControl_Resize()
        Dim lngRect(3) As Long
        GetWindowRect Parent.hWnd, lngRect(0)
        Debug.Print lngRect(2) - lngRect(0), lngRect(3) - lngRect(1)
    End Sub
    Last edited by Merri; Nov 15th, 2008 at 07:37 AM.

  4. #4

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Usercontrol on MDI Child cant get width/height of its parent

    Both works!
    Thanks a lot!

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