在显示父窗体在C#中的中心子窗体 [英] Show a child form in the centre of Parent form in C#

查看:241
本文介绍了在显示父窗体在C#中的中心子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的形式,并从父窗体调用如下:

I create a new form and call from the parent form as follows:

loginForm = new SubLogin();   
loginForm.Show();

我需要在父母的中心显示的子窗体。因此,在子窗体的负载我做FOLL:`

I need to display the child form at the centre of the parent. So,in the child form load I do the foll:`

Point p = new Point(this.ParentForm.Width / 2 - this.Width / 2, this.ParentForm.Height / 2 - this.Height / 2);
this.Location = p;

但是,这是抛出错误的父窗体为空。我试着设置Parent属性为好,但没有帮助。在这个任何投入?

But this is throwing error as parent form is null. I tried setting the Parent property as well, but didn't help. Any inputs on this?

推荐答案

尝试:

loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(this);

当然,孩子现在会被父窗口的拦截形式(对话),如果不希望那么就替换的ShowDialog 显示 ..

loginForm.Show(this);

您仍需要虽然指定中StartPosition

You will still need to specify the StartPosition though.

这篇关于在显示父窗体在C#中的中心子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆