473,378 Members | 1,504 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,378 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 4703
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
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.