简体   繁体   English

UserControl将Form带到前面

[英]UserControl bring Form to front

Is there a way for a Custom UserControl to bring the Main Form of the application to the front? 有没有办法让Custom UserControl将应用程序的主窗体带到前面?

Basically I'm integrating with some 3rd party libraries that every time that I call them that application is brought in front of everything else. 基本上我正在与一些第三方库集成,每当我称之为应用程序被带到其他一切之前。 I'm wanting to allow the custom control to request the main form bring itself back on top of everything. 我想让自定义控件请求主窗体使自己回到最重要的位置。

I had considered catching the Leave event from the main form, but this doesn't seem like it would be a good idea. 我曾考虑从主表单中捕获Leave事件,但这似乎不是一个好主意。

Program hierarchy... 程序层次......

MainForm > TabControl > TabPages[] > each as a Custom UserControl

I'd prefer to not have to make the parent or child aware of the other (unless that's the recommended practice). 我宁愿不必让父母或孩子知道对方(除非这是推荐的做法)。 My normal development is web based so who talks to who and proper design of a Windows Forms app is still new to me. 我的正常开发是基于网络的,所以谁谈论谁和适当的Windows窗体应用程序设计对我来说仍然是新的。

You can try 你可以试试

Application.OpenForms[0].Activate() // or BringToFront()

But keep in mind, that you should have any opened forms, before you start your main form in Main method: 但请记住,在Main方法中启动主窗体之前,您应该有任何已打开的窗体:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// do not open any forms here
Application.Run(new MainForm());

If you really need to have some forms opened before main form, then this will go: 如果你真的需要在主表单之前打开一些表单,那么这将是:

Application.OpenForms["MainForm"].Activate()

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

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