Link to home
Start Free TrialLog in
Avatar of clo1
clo1

asked on

Center a Child Form in MDI form

I have a MDI form, and a Child Form, and i am using the function as following to center the Child form

Public Function Center(Parent As Form, Child As Form)
    Child.Left = (Parent.Width - Child.Width) \ 2
    Child.Top = (Parent.Height - Child.Height) \ 2
End Function

And the problem is every time when i load up the MDI form and the Child form, the Child form has to shift 2 position beofre it goes to the center. It is pretty obvious, so what i want is how to center the form right in the center when the form load up. So it doesn't need to position 2 position before center itself. Thanks for answer !
Avatar of SLE
SLE

First load the form, afterwards show the form:

Dim objChild as Form
Set objChild=New frmChild
Load objChild
...
objChild.Show


Amazing. I was going to retort with >>why don't you just set the StartUpPosition of the child form to "1 - CenterOwner"<< only to find out that it won't allow you! So SLE is absolutely right.
Avatar of clo1

ASKER

Can you give me more details? Because it still have the problem. I don't know if i have do anything wrong or the code is not correct. Please post the answer again. Thanks !!!
You should include your code within SLE's answer:

Dim objChild as frmChild
Set objChild=New frmChild
Load objChild

    objChild.Left = (yourmdiform.Width - objChild.Width) \ 2
    Child.Top = (yourmdiform.Height - objChild.Height) \ 2

objChild.show
Hehehe caraf_g, right! :-))
- I'll leave it for you to answer, SLE
' #VBIDEUtils#************************************************************
' * Programmer Name  : Ramanan Gunendran
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 7/05/99
' * Time             : 14:10
' **********************************************************************
' * Comments         : Center MDI Child in MDI Form
' *
' * Assumes:MDI Form Name - MDIMain
' * MDI Child Form Name - frmMDIChild
' * Set the MDIChild Property In frmMDIChild form to False
' * Set the StartupPosition Property in frmMDIChild form to center owner
' *
' **********************************************************************

Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

SetParent frmMDIChild.hwnd, MDIMain.hwnd

Avatar of clo1

ASKER

The answer from SLE caraf_g is more stable. But thanks anyway
This code you will have to write in child Forms Load event as well as Resize event.

ChildForm.left = (MDIForm.Width - ChildForm.Width)/2
ChildForm.Top = (MDIForm.Height - ChildForm.Width)/2

Check the scale mode for both Child and MDI Forms it should be same.
this way maximum you can center your form.
apratima, I'm sure you mean well, but what you are doing could be interpreted as "point grabbing" and some participants can react rather furiously to it.

If you'd read the full question history, you would have realised that SLE has proposed the correct solution, and deserves the points. I added a comment which allows clo1 to implement SLE's solution.

What's more, clo1's last comment was an invitation to SLE to answer the question and get the points. More reason not to answer the question if you're anybody else. Now clo1 will have to reject your answer and invite SLE again. Very irritating.
Avatar of clo1

ASKER

Yeah, SLE can fix my problem correctly. The points is for him. But thanks for answer anway !!
ASKER CERTIFIED SOLUTION
Avatar of SLE
SLE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is too much. I am not trying to grab any points. I was just trying to help wchich is the main purpose of this web site I think
apratima, your help is very much appreciated, so please don't take offence.
Create you a module for all of your form alignments. For centering a MDI child, set the MDI Child to true, place the following code in the Center module:

Public Sub FormCenterMDI(frmAny As Form)
    frmAny.Top = (frmMain.ScaleHeight / 2) - frmAny.Height / 2)
    frmAny.Left = (frmMain.ScaleWidth / 2) - (frmAny.Width / 2)
End Sub


Then Create a sub within the form and call it FormInit():


Private Sub FormInit()
   Call FormCenterMDI(Me)
End Sub

Then in the form_load enter:

Call FormInit

This works great for me.