How to make mdichild load at the center of mdiparent window

烈酒焚心 提交于 2019-12-01 17:59:31

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.

Setting start position as center screen works perfect for me.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!