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.

Recommended Answers

All 7 Replies

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 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.



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);

Hope this helps someone out there!

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

"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

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

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.