Mars base one Username: Jim Davis Password: yCrm33
Posts
1,284
[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.
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
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