0
votes

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.

1
When this controls need to be brought to front? not b user-click I guess...so do they do some sort of calculation and when finished they need to be brought to front? I am sorry but I didn't got the idea you want to implement! - Saeid Yazdani
@Saeid87 I'm integrating with a Document Management System that we have On Site. We currently are using a VBScript to automate a few tasks, but recently we have needed some functionality beyond what VBScript can provide (at least with the current developers we have (mainly me)) so I've been rewriting it to have that functionality inside of a C# Windows Forms app. The DMS takes focus whenever it performs a function. I need the child form (the one that is aware it made a call) to be able to request focus again. This is simply a usability feature for user so they don't have to click back. - Jared

1 Answers

2
votes

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