Centering a MDI Child Form

Z

Zack Sessions

I am using VB.NET 2003.

I have read the threads concerning the problem where the
FormStartPosition of CenterParent is ignored if the form is displayed
with the Show method as opposed to the ShowDialog method. I am having
a similar problem and I found a thread that had the following code in
the child form's Load event:

Me.Location = New Point(( _
Me.MdiParent.ClientSize.Width - Me.Width) * 0.5,
_(Me.MdiParent.ClientSize.Height - Me.Height) * 0.5)

My code stream is a little convoluted. In the app's main form, I call
a function that is in a Module from a command button OnClick event. In
that function I open the child form that I would like to center over
the app's main form. The problem is, that in the context of the called
function (apparently since it is located in a module and not a
function in the main form's class) the MdiParent is undefined and the
centering code returns an error.

I know moving the called function from the module to the main form's
class would probably fix this problem, but I am reluctant to resort to
that.

Any suggestions?
 
I

Imran Koradia

You could pass a reference to your main form to the function in the module
and then assign it to the MdiParent property of the child form. Then the
code you mentioned should work ( I haven't tried this but worth a shot..)

' This is in your main app form..
Private Sub btn_Click(sender As Object, _
e As System.EventArgs)
myFunctionInModule(DirectCast(Me, Form))
End Sub

Public Module myModule
Public Sub myFunctionInModule( _
ByVal Parent As Form)
Dim Child As New Form
Child.MdiParent = Parent
' your positioning code...
Child.Show( )
End Sub
End Module


hope that helps..
Imran.
 
Z

Zack Sessions

Imran Koradia said:
You could pass a reference to your main form to the function in the module
and then assign it to the MdiParent property of the child form. Then the
code you mentioned should work ( I haven't tried this but worth a shot..)

Thanks for your response. I finally got around to trying it today and
I am having a problem. See comment below in your code:
' This is in your main app form..
Private Sub btn_Click(sender As Object, _
e As System.EventArgs)
myFunctionInModule(DirectCast(Me, Form))
End Sub

Public Module myModule
Public Sub myFunctionInModule( _
ByVal Parent As Form)
Dim Child As New Form
Child.MdiParent = Parent

This state gets the following error:

The form that was specified to be the MdiParent for this form is not
an MdiContainer.

Any ideas?
 
I

Imran Koradia

Public Module myModule
This state gets the following error:

The form that was specified to be the MdiParent for this form is not
an MdiContainer.

Your title mentions you're trying to position an MDI child form and so I
assume your parent form is an MdiParent (the IsMdiContainer property is set
to True). Is that not the case?

1. If your parent is an mdi container (in which case it will be the
mdiparent of your child form), you can do as I've mentioned in the code
above. To center the mdi child in the center of the mdi parent, you don't
need any code - just set the StartPosition property of the child form to
FormStartPosition.CenterScreen and the child will always start up at the
center of the mdi parent.

2. In the case that the parent is NOT and mdi container, and you still want
to position this newly created form in the center of the main form, you can
do this:

Public Module myModule
Public Sub myFunctionInModule( _
ByVal Parent As Form)
' replace "ChildForm" with whatever your
' child form name is..
Dim Child As New ChildForm
Child.Location = New Point(Parent.Location.X + _
CInt((Parent.ClientSize.Width - o.Width) * 0.5), _
Parent.Location.Y + _
CInt((Parent.ClientSize.Height - o.Height) * 0.5))
Child.Show()
End Sub
End Module

The code in your main form stays the same.

hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top