473,225 Members | 1,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Centering a MDI Child Form

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
3 4694
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
"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
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pierre Jelenc | last post by:
Is there a way to center (horizontally) a UL list of unknown width? I can put it in a DIV that's centered with "margin-left: auto; margin-right: auto;" but I then have to specify a width; otherwise...
3
by: CMAR | last post by:
To center my <div>, I am currently using something like <div id="navcontainer" align="center"> This centers fine, but is deprecated code. I have seen these two solutions recommended. 1) ...
2
by: Lee K. Seitz | last post by:
I stayed up late working on a CGI script and spent some time formatting my page with CSS. It looked fine in IE, which will be 95% of my audience, so I went to bed. Got up and looked at it in...
2
by: Brian Henry | last post by:
is it possible to center a child form in a mdi window? i tried the centerparent for the position enumerated value, but it didn't do anything.. any thoughts? thanks
0
by: mabond | last post by:
Hi I'm having a problem centering my child window within its parent. It is opening docked top left. Form properties as follows: frmMain (IsMdiContainer = true) frmPCASelection...
5
by: Markus Ernst | last post by:
Hello This is a test example: http://www.markusernst.ch/anthracite/ http://www.markusernst.ch/anthracite/living_divani.html After googling and experimenting for several hours, I ended up...
2
by: rudicheow | last post by:
SHORT VERSION ============= I have a bunch of identical fixed-size single-celled tables that rest against each other horizontally thanks to "float:left". These tables are dynamically generated...
3
by: Peted | last post by:
I have a simple c# app that opens a child form in a mdi parent. I want the child form to open in the center of the mdi parent, so i can open the child form ok, it all works fine, but setting ...
1
by: =?Utf-8?B?ZnJhbmt5?= | last post by:
Hello, I've created a table that has two rows that are span across three columns. The third row has three columns, each with an image. The last row is also span accross three columns. The span...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.