Child Parent Forms -- CenterParent

GreenKawasaki

Newcomer
Joined
Dec 23, 2003
Messages
6
Hello,

How do I setup my forms so I can use that 'CenterParent' option for forms? I have a mainform and a bunch of smaller forms that I want to popup in the middle of the main form no matter where it is on the screen.

Any help is greatly appreciated! I currently have my mainform's IsMDIContainer set to true.

Visual Basic:
Dim MyForm As New SmallForm()

MyForm.StartPosition = FormStartPosition.CenterParent
MyForm.ShowDialog()

Unfortunately, this doesnt work.

Best regards,
VB Newbie
 
Last edited:
you need to tell your form ( MyForm ) what it's MdiParent is.
then after showing the form, you need to set it's new location.
for some reason using the CenterScreen start position doesnt work. here's a quick example of how i got one running...
Visual Basic:
        Dim MyForm As New SmallForm()
        MyForm.MdiParent = DirectCast(Me, Form1) '/// set MyForm's parent here.
        MyForm.Show()
        Dim x As Integer = (Me.Width / 2) - (MyForm.Width / 2)
        Dim y As Integer = (Me.Height / 2.2) - (MyForm.Height / 2) '/// allow extra for the toolbox ( hence 2.2 )
        MyForm.Location = New Point(x, y)'///Center the form in it's parent.
 
Dynamic_sysop,

Thanks for the fast reply!

This code finally solves my problem with centering forms. (I'm not really sure why .net doesn't make this easier) I really appreciate your help.

Best Regards,
GreenKawasaki
 
Or it's me who is missunderstanding something here or... I don't know...

If you have a MDIChild and you want it to show in the center of the MDIParent you have to set:
Visual Basic:
ChildForm.MDIParent = MDIParent
ChildForm.StartPosition = FormStartPosition.CenterScreen
ChildForm.Show
Cause the work screen for this child form it's the MDIParent WorkSpace...

If, in the other hand, you have a FormA and whant to display a FormB with ShowDialog, not MDIChild, centered in FormA you'll have to use:
Visual Basic:
FormB.StartPosition = FormStartPosition.CenterParent
FormB.ShowDialog(FormA)
This is the StartPosition Logic...

Alex :D
 
I had problems with VB not consistently centering my form in its parent too. So I created the follow bit and stuck it in a module for reusage.

Sub CenterInParent(ByVal parent As Form, ByVal child As Form)
Dim frmA As Form = parent
Dim frmB As Form = child
Dim x As Integer = (parent.Width / 2) - (child.Width / 2)
Dim y As Integer = (parent.Height / 2.2) - (child.Height / 2)
child.Location = New Point(x, y)
End Sub
 
I think the issue is that .NET centres the window to the MDI parent based on the 'size' values that you set for the MDIParent properties.

When you maximise the MDIParent, the 'size' values do not change, therefore 'CenterParent' will not give the desired result.

I have always used CenterScreen with no problems so far.


Code:
Sub CenterInParent(ByVal parent As Form, ByVal child As Form)
Dim frmA As Form = parent
Dim frmB As Form = child
Dim x As Integer = (parent.Width / 2) - (child.Width / 2)
Dim y As Integer = (parent.Height / 2.2) - (child.Height / 2)
child.Location = New Point(x, y)
End Sub

This will also solve the problem, and will always find the middle of the Parent in its current state (maximised or not).
 
Back
Top