问题
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
回答1:
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.
回答2:
Setting start position as center screen works perfect for me.
回答3:
I tried showing the child with the MDI container form as owner, but caused an exception for me. You could manually set the location before showing the child as follows:
Form2 f = new Form2();
f.MdiParent = this;
f.StartPosition = FormStartPosition.Manual;
f.Location = new Point((this.ClientSize.Width - f.Width) / 2,
(this.ClientSize.Height - f.Height) / 2);
f.Show();
EDIT:
f.StartPosition = FormStartPosition.CenterScreen;
is the correct way to center an mdichild on its parent form.
回答4:
A form has an MdiParent property and a Parent property.
Please notice that an MdiParent IS NOT the parent of MdiChild form.
It is, its MdiParent;
So CenterToParent Method is meaningless for these types of forms.
You can see the functionality of CenterToParent Method when you program for multiple monitors application, or when you host a form inside another form by setting its Toplevel property to false, and setting its Parent property to another form.
Good Luck,
Aram Afsar
来源:https://stackoverflow.com/questions/1228120/how-to-make-mdichild-load-at-the-center-of-mdiparent-window