简体   繁体   中英

UserControl bring Form to front

Is there a way for a Custom UserControl to bring the Main Form of the application to the front?

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.

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.

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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