473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Centering a MDI Child Form

I am using VB.NET 2003.

I have read the threads concerning the problem where the
FormStartPositi on 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.Cl ientSize.Width - Me.Width) * 0.5,
_(Me.MdiParent. ClientSize.Heig ht - 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 4736
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(sende r As Object, _
e As System.EventArg s)
myFunctionInMod ule(DirectCast( Me, Form))
End Sub

Public Module myModule
Public Sub myFunctionInMod ule( _
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********@vis ionair.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
I am using VB.NET 2003.

I have read the threads concerning the problem where the
FormStartPositi on 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.Cl ientSize.Width - Me.Width) * 0.5,
_(Me.MdiParent. ClientSize.Heig ht - 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****@microso ft.com> wrote in message news:<em******* *******@TK2MSFT NGP12.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(sende r As Object, _
e As System.EventArg s)
myFunctionInMod ule(DirectCast( Me, Form))
End Sub

Public Module myModule
Public Sub myFunctionInMod ule( _
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********@vis ionair.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
I am using VB.NET 2003.

I have read the threads concerning the problem where the
FormStartPositi on 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.Cl ientSize.Width - Me.Width) * 0.5,
_(Me.MdiParent. ClientSize.Heig ht - 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 myFunctionInMod ule( _
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
FormStartPositi on.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 myFunctionInMod ule( _
ByVal Parent As Form)
' replace "ChildForm" with whatever your
' child form name is..
Dim Child As New ChildForm
Child.Location = New Point(Parent.Lo cation.X + _
CInt((Parent.Cl ientSize.Width - o.Width) * 0.5), _
Parent.Location .Y + _
CInt((Parent.Cl ientSize.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
7237
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 the auto values get set to 0 and there is no centering. The list is generated dynamically, and there is no way to tell in advance how long each item is going to be. Alternatively, is there a good formula to determine with a reasonable...
3
4634
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) body { text-align: center; min-width: 500px;
2
2204
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 Opera and it's different and not what I wanted. Take a look at <http://home.hiwaay.net/~lkseitz/hhs/rnb/>. I'm concerned with the form and links in the dashed box in the middle of the page. In IE, my "margin: auto" causes the div to center itself...
2
1114
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
1036
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 (StartPosition = CenterParent)
5
4824
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 doing this demo with tables. The main problems are the vertical centering of the info area, and of the text inside the squares.
2
3062
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 and the number of them can vary greatly. They are all contained within a parent table cell, which is centred on the page. But I am unable to find a way of centering these table cells within the parent cell.
3
4308
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 frmConfig = new ConfigForm(this); frmConfig.MdiParent = this; frmConfig.TopLevel = false; frmConfig.StartPosition = FormStartPosition.CenterParent;
1
2287
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 rows are centering their data. however, the row with three columns, each with images (myimages1-3) are not centering with the rest of the table. Any idea why? Thanks in advance!
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.