Hi.
I want to center an MDI Child Form in a Maximized MDI Parent Form. How do I get the Height and Width of the Maximized Parent so that I can center the child?
Thx.
Printable View
Hi.
I want to center an MDI Child Form in a Maximized MDI Parent Form. How do I get the Height and Width of the Maximized Parent so that I can center the child?
Thx.
Try This:
Code:Dim x, y As Long
x = mdiparent.Width
y = mdiparent.Height
mdich.Left = (x - mdich.Width) / 2
mdich.Top = (y - mdich.Height) / 2
Thanks for the response. But using that technique only returns the Pre-Maximized height and width of the Parent form. Thus the child form won't be centered in the Maximized form. It will only be centered in the form's "Normal" state.
Maybe what I'm looking for can't be done. I guess I can use the klunky solution of making my parent form the same size that it would be in a maximized state. And then I could use your code Vlatko.
Thanks.
If you need to keep a tab on what width and height a form is at during runtime, use scalewidth/height instead of height/width.Code:Private Sub Form_Load()
x = MDIForm1.ScaleWidth
y = MDIForm1.ScaleHeight
Me.Left = (x - Me.Width) / 2
Me.Top = (y - Me.Height) / 2
End Sub
My program behaves no differently whether I use Height and Width or ScaleHeight and ScaleWidth. :confused: The child form is only centered in the normal state of the Parent window, regardless of the method I use. :( Do you have to put in some other "Scale" value in the parent form for this to work?
Thanks.
This will move it in the center of the MDIForm regardless of what size the parent Form is.
Code:Move (MDIForm1.ScaleWidth / 2) - (Me.ScaleWidth / 2), (MDIForm1.ScaleHeight / 2) - (Me.ScaleHeight / 2)
Megatron,
Your code only centers the child form in the parent form in the "Normal" window state of the parent form. Is there some scale property value I'm not setting to get this to work? This doesn't make sense. :confused: Here's the code in my app.Anyone see anything wrong with it?Code:Move (frmParent.ScaleWidth / 2) - (Me.ScaleWidth / 2), (frmParent.ScaleHeight / 2) - (Me.ScaleHeight / 2)
Thanks.
Nope. It works whether it's maximized or normal. Try placing that code in a button and press it. It should center itself.
Thanks for everyone's help.
I was able to get the code to work by placing it in the Activate event of the child form, rather than in the Load Event. I guess it wasn't working before because the Form was loading too quickly for the maximized properties to register?
Anyway, I would like to have it appear in the center of the form without seeing it first in the upper-left corner. It appears there for a split second and then jumps to the center. I tried using the Sleep API and making the form invisible (along with a DoEvents) until after the move, but neither worked.
Does anyone know how to prevent the child form from appearing until it is in the center of the parent form?
Thanks for the assistance.