Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Center a Child Form in MDI form

Posted on 1999-06-23
15
Medium Priority
?
488 Views
Last Modified: 2008-03-17
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 !
0
Comment
Question by:clo1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +3
15 Comments
 
LVL 2

Expert Comment

by:SLE
ID: 1519369
First load the form, afterwards show the form:

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


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1519370
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.
0
 
LVL 2

Author Comment

by:clo1
ID: 1519371
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 !!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:caraf_g
ID: 1519372
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
0
 
LVL 2

Expert Comment

by:SLE
ID: 1519373
Hehehe caraf_g, right! :-))
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1519374
- I'll leave it for you to answer, SLE
0
 
LVL 14

Expert Comment

by:waty
ID: 1519375
' #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

0
 
LVL 2

Author Comment

by:clo1
ID: 1519376
The answer from SLE caraf_g is more stable. But thanks anyway
0
 
LVL 1

Expert Comment

by:apratima
ID: 1519377
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.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1519378
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.
0
 
LVL 2

Author Comment

by:clo1
ID: 1519379
Yeah, SLE can fix my problem correctly. The points is for him. But thanks for answer anway !!
0
 
LVL 2

Accepted Solution

by:
SLE earned 0 total points
ID: 1519380
No offence - I'm not the furious type - but thanks anyway to y'all.
0
 
LVL 1

Expert Comment

by:apratima
ID: 1519381
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
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1519382
apratima, your help is very much appreciated, so please don't take offence.
0
 

Expert Comment

by:torobinson
ID: 1519383
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.


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 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