954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Child Form not Centering

I am in the process of developing an MDI application in VB.NET (2005). Anyway, my child form refuses to center the parent forms' bounds. I have tried setting the 'startposition' via the properties menu. I have also tried setting the 'startposition' with code in the parent form's load procedure. Here is the code I used.

frmMain.StartPosition = FormStartPosition.CenterParent

This line is placed after I declare frmMain as a child form and before I call the Show method.

Note: frmMain is the name of the child form. This caused a bit of confusion in the WindowsForms forum. My parent form is named frmMDI. I see no reason as to why my code is not working. Whenever I run the application, the child form is loading in the default upper-left hand corner of it's parent form.

If anybody has a possible solution, I would greatly appreciate it. Thanks for any help.

B.H.Also, does anybody have a link to an article dealing with embedding forms within other forms. I vaguely remember reading an article on form embedding, but can't figure out where.

bcheath_1
Newbie Poster
20 posts since Jan 2005
Reputation Points: 10
Solved Threads: 1
 

I know you probably already tried this, but just for the record, try inserting a breakpoint and follow the code line by line to see if the child form is being placed in that position when it is loaded or some other code is changing its position. Don't forget to follow even the "Windows Forms Designer generated code" for the child form and the main form. some code could be hidden there that is changing the start position. That could help.

Thanks,
adrcam

adrcam
Newbie Poster
4 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

adrcam,

Thanks for the suggestion, but you were correct; I had already tried walking through the code. However, I was unable to find anything out of the ordinary. So, if anybody else has a suggestion, please share it.

Thanks,

bcheath_1

bcheath_1
Newbie Poster
20 posts since Jan 2005
Reputation Points: 10
Solved Threads: 1
 
waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

I know this thread is very old now, but I suspect there are still people out there struggling with this problem just like I've been... So I thought I'd do my bit of community service.

What I finally found to allow you to at least arbitrarily place a new MDI child window anywhere on an MDI parent is the following sequence after having created a new instance of the child form:

MDIchild->StartPosition = FormStartPosition::Manual;
MDIchild->Location = Point(newX,newY);
MDIchild->Show();

Hope this helps someone out there!

Scott Barry
Newbie Poster
1 post since May 2008
Reputation Points: 10
Solved Threads: 0
 

well great for help, but this section for vb.net so please write code in vb.net code not in c# friend :)

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

"Here's what I've used in the past to center MDI Child Forms

Dim MDIChildForm As New FormName 'Where FormName is the name of the MDI Child Form
MDIChildForm.MdiParent = Me
MDIChildForm.Show()

Dim BorderWidth As Integer = (Me.Width - Me.ClientSize.Width)
Dim BorderHeight As Integer = (Me.Height - Me.ClientSize.Height)

Dim ChildLeft As Integer = Round((Me.Width - BorderWidth - MDIChildForm.Width) / 2, 0)
Dim ChildTop As Integer = Round((Me.Height - BorderHeight - MDIChildForm.Height) / 2, 0)

MDIChildForm.Left = ChildLeft
MDIChildForm.Top = ChildTop

'DO NOT execute from 'NEW' Method
'
'Hope this Helps

mcspidy
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

You can do it like this to deside where in the mainform your childform should start:

In your mainform add this:
Public class yourclass
Public locat As Drawing.Point
Private sub form_load
locat = me.location

In your child form:
private sub form_load
me.location = mainformname.locat + new.point(x, y)

x and y sets where the childform should start in your mainform. It will start in the upper left corner.

Works perfect for me if I want a childform to completely cover my mainform, except for the frame around it. Just make the childform frameless... (formborderstyle = none)

Just another angle to look at the challenge :)

Mortimern
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You