Thursday, October 28, 2010

Single-instance application in VB 2008, prevent automatic focus on the main form?

I have a single-instance application in VB 2008. I want it to open a new window each time the executable is run, instead of simply bringing the currently running window to the foreground (the way Microsoft Word operates).



Currently, my program works as expected, with one big issue -- the first opened window is brought to the foreground, instead of the newly created secondary (or tertiary, etc) window.



How can I prevent the first window from automatically getting focus?Single-instance application in VB 2008, prevent automatic focus on the main form?
Back in the day we had to call ShowWindow or SetWindowPos to show a program's window -- both of which offered the option to show without activating. Sadly VB.NET forms do not permit the same flexibility, though you still might be able to do what you want here...



First assume that any form property that has to do with size or position could possibly affect this behavior. (You may need to experiment.)



Second, consider the obvious: if the form is not enabled by default (Enabled = False in design-time properties) then it can't possibly take focus when it opens. You can set a short timer to enable it after all the default load, position and show behavior has executed.



Whether setting Enabled = True after the fact will cause it to take focus, I'm uncertain -- if it does, it's lame! In that case you will have to look deeper into the framework (for an equivalent to the ancient ShowWindow API, of song and story.) :-)



Good Luck Dude! :-)
  • tvs
  • No comments:

    Post a Comment