Take the 2-minute tour ×

I have written an application which has a modal form. How can I ensure that this form does not lose the focus even when an other application is started?

share|improve this question
I'm assuming this is a web application. Is that correct? –  Pablo Fernandez Sep 27 '08 at 14:21
add comment

5 Answers

Actually, this is exactly the sort of thing you shouldn't be doing.

There's too many programs around that assume they control the computer they're installed on. It is the user of your application that should be in control.

That's why later versions of Windows disallowed stealing of focus instead insisting on just blinking the entry in the task list bar.

You may well find a way to do it (though I doubt it), but I urge you to rethink it. I'd be interested in knowing why you thought it was necessary.

share|improve this answer
I wrote an application to hear to web radio stations while I am on my bicycle ergometer. I can change the stations by pressing a button on the game pad that is fixed on the ergometer. When the focus of the application is lost I am no longer able to use the game pad. –  Mat Sep 27 '08 at 13:46
Why? How do you read the data from the game pad? –  OregonGhost Sep 27 '08 at 13:54
I use the directX-Framework. When the focus of the application is lost I can't read the data any more. –  Mat Sep 27 '08 at 14:31
Actually I retract my earlier comment: if it's software for your own use, you have a valid reason for keeping focus. Perhaps a question you should be asking is "how can I stop other apps from stealing my focus?" I would have thought that, while you're on your bike, no other apps would start. –  paxdiablo Sep 27 '08 at 14:48
There are different actions, for example i can start you tube videos with the application. some videos cause script errors and because of that the browser control shows a pop up and the focus of the main form is lost. –  Mat Sep 27 '08 at 15:00
show 1 more comment

You can set the "Topmost" property to true to keep the form in front of all others but that doesn't make it keep focus.

share|improve this answer
add comment

You must make the dialog system modal.

share|improve this answer
"You must the dialog system modal." needs to be re-worded to be clearer. –  John Sep 27 '08 at 13:57
add comment

I use


Me.Handle is the handle of your form.

You need to declare the following somewhere in your class or winform, but not inside a function

Declare Unicode Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean

You might need to initiate a timer and call SetForegroundWindow on every tick of say 10 seconds, depending on your preference.

EDIT: It works for me, if it doesn't add the following

    Declare Unicode Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoW" (ByVal uiAction As Int32, ByVal uiParam As Int32, ByRef pvParam As Int32, ByVal fWinIni As Int32) As Int32

And surround SetForegroundWindow with these

    Dim _timeout As Int32
    SystemParametersInfo(&H2000, 0, _timeout, 0)
    SystemParametersInfo(&H2001, 0, 0, 3)
    SystemParametersInfo(&H2001, 0, _timeout, 2)

That's the last resort

share|improve this answer
Won't work since XP, thankfully. –  Simon Buchan Sep 27 '08 at 14:29
add comment

I have to use an application that grabs focus when it pops up an alarm. I hate it! (If I had any choice, I would not use the damn thing; sadly, I have to use it. Ugh!) Other parts of the program are far too keen on maintaining focus. Some forms do not let you cut'n'paste text from other pages. It is an abomination when a program does that. If you want your software used by people who have a choice, then DO NOT grab and retain focus.

share|improve this answer
add comment

Your Answer

