从另一个子窗口显示一个新的子窗口,但将其置于父窗口的中心

发布于 2024-12-11 11:02:20 字数 1109 浏览 0 评论 0原文

我有一个 winforms 应用程序,带有一个主窗体(fMain)和两个子窗体(fLogin 和 fAdmin)。 fLogin 使用以下代码显示,该代码位于主窗体上按钮的按钮单击事件处理程序中:

// show login form; pass the main form in as an argument
fLogin formLogin = new fLogin(this);
formLogin.StartPosition = FormStartPosition.CenterParent;
formLogin.ShowDialog(this);

在 fLogin 的构造函数中,我将主窗体分配给私有成员级别变量。

// fLogin
fMain _mainForm;

// fLogin constructor
public fLogin(fMain mainForm)
{
    InitializeComponent();
    _mainForm = mainForm;
}

正如您可能想象的那样,fLogin 是一个小型表单,其中包含用于输入用户名和密码的文本框以及几个按钮。当我的用户输入他们的凭据并单击“确定”按钮时,fLogin 将通过服务器验证信息,如果信息正确,fLogin 将消失并显示 fAdmin。目前,我像这样显示 fAdmin:

// hide formLogin right away
this.Hide()

// show admin form
fAdmin formAdmin = new fAdmin();
formAdmin.StartPosition = FormStartPosition.CenterParent;
formAdmin.Show(_mainForm); // pass main form as owner of admin form

// close formLogin
this.Close();

我无法设置 formAdmin.Parent = _mainForm 并使对话框神奇地居中。因此,我将 _mainForm 作为 formAdmin 的所有者传递给 formAdmin.Show() ,但这似乎对以 formAdmin 为中心没有帮助。有没有一种简单的方法可以使formAdmin显示在主窗体的中心?

I have a winforms app with a main form (fMain) and two child forms (fLogin and fAdmin). fLogin is displayed using this code which is in a button click event handler for a button on the main form:

// show login form; pass the main form in as an argument
fLogin formLogin = new fLogin(this);
formLogin.StartPosition = FormStartPosition.CenterParent;
formLogin.ShowDialog(this);

In the constructor for fLogin, I assign the main form to a private member level variable.

// fLogin
fMain _mainForm;

// fLogin constructor
public fLogin(fMain mainForm)
{
    InitializeComponent();
    _mainForm = mainForm;
}

As you might imagine, fLogin is a small form with textboxes for a username and password and a couple of buttons. When my users enter their credentials and click the OK button, fLogin will validate the information with a server and if the information is good, fLogin will disappear and fAdmin will be displayed. Currently, I'm displaying fAdmin like this:

// hide formLogin right away
this.Hide()

// show admin form
fAdmin formAdmin = new fAdmin();
formAdmin.StartPosition = FormStartPosition.CenterParent;
formAdmin.Show(_mainForm); // pass main form as owner of admin form

// close formLogin
this.Close();

I can't set formAdmin.Parent = _mainForm and get the dialog to magically center itself. So I'm passing _mainForm to formAdmin.Show() as the owner of formAdmin but that doesn't seem to help with regard to getting formAdmin centered. Is there an easy way to cause formAdmin to be displayed at the center of the main form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-12-18 11:02:20

我认为您需要稍微调整一下执行此操作的方式,而不是从 fLogin 中显示 fAdmin,而是关闭 fLogin,然后从 fMain 打开 fAdmin。如果这不起作用,您可以通过计算 fAdmin 左上角的点并将该点设置为 fAdmin 的位置来手动居中。当我过去遇到类似的问题时,我不得不这样做。要计算 fAdmin 的左上角,使其以 fMain 为中心,请使用以下命令:

Point p = new Point(0, 0);
p.Y = (fMain.Height / 2) - (fAdmin.Height / 2);
p.X = (fMain.Width / 2) - (fAdmin.Width / 2);
fAdmin.Location = p;

I think you need to restructure how you are doing this just a bit, instead of displaying fAdmin from within fLogin, close fLogin and then open fAdmin from fMain. If this doesn't work you can manually center if by calculating the point for the upper left corner of fAdmin and set this point as fAdmin's Location. I've had to do this in the past when I've encountered similar issues. To calculate the upper left corner for fAdmin so that it will be centered on fMain, use the following:

Point p = new Point(0, 0);
p.Y = (fMain.Height / 2) - (fAdmin.Height / 2);
p.X = (fMain.Width / 2) - (fAdmin.Width / 2);
fAdmin.Location = p;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文