Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make mdichild load at the center of mdiparent window

Tags:

c#

mdi

HI all,

I have this code in which the window property of making child window load at the center of mdiparent.

        Form2 f = new Form2();

        f.MdiParent = this;

        //center screen is working.

        //f.StartPosition = FormStartPosition.CenterScreen;

        f.StartPosition = FormStartPosition.CenterParent;

but instead of making the child window pop at the center it loads at the left side. Can anyone help me on this. Please refer the screenshot below.

I have even tried doing the same in vb. Even there i get the same error. I think the property of FormStartPosition.CenterParent is dummy.

alt text http://img13.imageshack.us/img13/7003/errorprb.jpg

like image 254
Srikanth V M Avatar asked Aug 04 '09 15:08

Srikanth V M


People also ask

What is MDI child window?

The MDICLIENT window manages the client area of the main frame window, and itself has child windows: the document windows, derived from CMDIChildWnd . Because the document windows are frame windows themselves (MDI child windows), they can also have their own children.

Which is the correct code to add child form in parent form?

Click on New Menu Item and generate a Click Event Handler on it. Now Double click on New Menu Item and Write down the following Code. Form2 newMDIChild = newForm2(); // Set the Parent Form of the Child window.


2 Answers

I experimented a bit with this, and first came to the same solution as Patrick. But I was bugged by the following statement in the documentation of the StartPosition property:

You can also position the form to display in the center of the screen or in the center of its parent form for forms such as multiple-document interface (MDI) child forms.

So, I decided that there must be a way. And there is, though I don't find it all intuitive: set the StartPosition to CenterScreen (not CenterParent):

MdiChildUI form = new MdiChildUI();
form.MdiParent = this;
form.StartPosition = FormStartPosition.CenterScreen;
form.Show();

Of course, you can also set it in the designer instead of in code.

like image 162
Fredrik Mörk Avatar answered Sep 25 '22 14:09

Fredrik Mörk


Setting start position as center screen works perfect for me.

like image 30
danish Avatar answered Sep 24 '22 14:09

danish