简体   繁体   English

如何将胜利形式摆在面前?

[英]How to bring a win form to front?

I have a winform that drives me crazy... When I open it, it opens and goes behind the main form from which I open it. 我有一个winform会让我发疯……当我打开它时,它会打开,并且位于打开它的主要窗体的后面。 I tried calling: BringToFront(), Focus(), Activacte() and still nothing.... Here is my code: 我尝试调用:BringToFront(),Focus(),Activacte()还是一无所有。...这是我的代码:

MyForm frm = new MyForm();
frm.WindowState = FormWindowState.Maximized;
frm.StartPosition = FormStartPosition.CenterParent;
frm.Show();
frm.BringToFront();
frm.Activate();

I know that the main form has the TopMost property set to true, but I also have this code that works just fine: 我知道主窗体的TopMost属性设置为true,但是我也有这段代码可以正常工作:

Form frmLog = new Form();
LogViewer logControl = new LogViewer();
frmLog.Controls.Add(logControl);
logControl.Dock = DockStyle.Fill;
frmLog.WindowState = FormWindowState.Maximized;
frmLog.StartPosition = FormStartPosition.CenterParent;
frmLog.Text = "Log Viewer";
frmLog.Show();

So that is way it drives me crazy... 所以这让我发疯...

You have to set your main form as owner for frm by using Form.Show(IWin32Window) method. 您必须使用Form.Show(IWin32Window)方法将主窗体设置为frm的所有者。

So your code should look like 所以你的代码应该看起来像

frm.Show(this);

You have to pass the instance of the main form to the Show method of the form you want to show. 您必须将主窗体的实例传递给要Show窗体的Show方法。

MyForm frm = new MyForm();
frm.Show(this);

Of course this works if you are instanciating the second form inside the main form class (so you can use the this keyword. 当然,如果您要在主窗体类中实例化第二个窗体,则此方法有效(因此可以使用this关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM