By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,578 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,578 IT Pros & Developers. It's quick & easy.

Centering a MDI Child Form

P: n/a
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?
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"Zack Sessions" <zc********@visionair.com> wrote in message
news:db**************************@posting.google.c om...
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?

Nov 21 '05 #2

P: n/a
"Imran Koradia" <no****@microsoft.com> wrote in message news:<em**************@TK2MSFTNGP12.phx.gbl>...
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?
' your positioning code...
Child.Show( )
End Sub
End Module
hope that helps..
Imran.

"Zack Sessions" <zc********@visionair.com> wrote in message
news:db**************************@posting.google.c om...
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?

Nov 21 '05 #3

P: n/a
> > 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.


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.
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.