0

I have a WinForms Form I call from two places, one from a tray-icon's context menu and from a context menu I get from right-clicking on my app's shortcut on the desktop. When I use the tray-icon's menu the form opens just fine, even if it was minimized - it displays again. The problem is when I use the link. If the form is minimized, sometimes it will restore it, but other times it won't. The application's icon on the task-bar will flash but the window itself won't show.
I checked through debugging and both calls to display the form are from the same thread. Are there other contexts I'm missing that might interfere?

Code I use:

public void Display()
{
    this.WindowState = FormWindowState.Normal;
    this.Show();
    this.Activate();            
    this.Focus();
}
6
  • 3
    Did you try this.BringToFront()? Jul 3, 2014 at 12:27
  • Yes, it didn't work. The problem isn't that it's hidden behind other windows, but that it doesn't restore from being minimized.
    – Ady
    Jul 3, 2014 at 12:35
  • Sadly, this is the expected behaviour on Windows from Vista on. Basically, it's supposed to protect the user from having his work interrupted by popup windows from applications he isn't using at the moment (the dreaded focus stealing). It should be possible to override this somehow in your case, but you probably have to give focus to the existing window from the process the user has started. How are you handling the startup from the desktop icon?
    – Luaan
    Jul 3, 2014 at 12:37
  • More things you could try: stackoverflow.com/questions/354445/… Jul 3, 2014 at 12:40
  • 1
    The "shortcut" clicking is very murky, that start a new process. One that's entirely separate from the one that you already have running. Writing a single-instance app is very possible, there's no evidence at all that you actually did so. Using the WindowsFormsApplicationBase class is the trivial solution, it fires the StartupNextInstance() method in the original process. Jul 3, 2014 at 14:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.