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

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

问题描述

我创建一个新表单并从父表单调用,如下所示:

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

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

我需要在父窗体的中心显示子窗体.所以,在子表单加载中,我做了以下操作:`

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 替换为 Show..

Of course the child for will now be a blocking form (dialog) of the parent window, if that isn't desired then just replace ShowDialog with Show..

loginForm.Show(this);

尽管如此,您仍然需要指定 StartPosition.

You will still need to specify the StartPosition though.

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

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