First of all, the problem has nothing to do with the visibility of the taskbar icon representing your form, but I understand that you are asking because you have nothing to click on. You can do it programmatically, but why?
It's a complex of problems, so I just list some things you need to care for; and you try to put it together yourself, because your problem is not clearly explained:
- Forms are not "focused" but are activated:
https://msdn.microsoft.com/en-us/library/system.windows.yforms.form.activate%28v=vs.80%29.aspx[^]. - You shouldn't loose the active state of a form in a modal state, unless you switch to another application. There is no a reason to force activation of your application's form; the user knows better. If the user switches to another application, assume it's done on purpose.
- You should better maintain the Z-order of all application forms in respect to the switching to other applications. Al you forms should come together in Z-order of the desktop. You can easily achieve it if you maintain owner-owned relationship between all forms: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner%28v=vs.80%29.aspx[^] (see also other related members, but this one would be enough).
Practically, it would be enough to make a main form an owner of all other forms and keep the main form always visible (I don't mean it cannot be covered by other forms of your application or other applications, it can). Then, no matter of what form is on top in Z-order, when you activate any form of your application, it will move all other forms on top in some order, so none of the windows of other applications will sneak in between. - It's good to couple the previous technique with the use of
Form.ShowInTaskbar
:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar%28v=vs.80%29.aspx[^].
The base design matching the above would be this: make this property true for your main form, false for all other forms; this way, if the user activates your application using the taskbar or not (taskbar will always show only one item per your application, the only one to click on), some of your forms will be activated, if the application is showing a modal form at this moment, the top-level modal form will be activated. That should solve your problem.
At the same time, you can deviate from this style, intentionally adding Form.ShowInTaskbar
true for some other select form, such as your modal form. I don't like this style, because it will give the user a choice what to click on, and one click would make no sense. You could also maintain the following rule: only one form at a time should have this property equal to true. But I would not bother; the simplest rule showing only the main form in the taskbar is good enough. - Even better, prefer the design with only one main form. What was other forms, could become some controls withing the main form, it can be panels (showing and hiding, optionally resizing),
TabPage
instances in an instance of TabControl
, and so on. I don't mean some more modal form; you can have some small number of them, also don't overdo it. Such design is the easiest to navigate and understand. You can even develop the UI similar to Visual Studio, with dynamic docking. - At the same time, never use MDI. Just don't.
If you need more specific advice, you have to provide more detailed information.
Good luck.
—SA