How to prevent my C# winforms application from stealing focus when I set the visible properly to tru

Posted by Fopedush on Stack Overflow See other posts from Stack Overflow or by Fopedush
Published on 2010-05-15T03:43:46Z Indexed on 2010/05/15 3:54 UTC
Read the original article Hit count: 274

Filed under:
|
|
|

I've got a C# winforms application that runs in the background, listening for hotkeys to be pressed. When a hotkey is pressed, my form makes a brief appearance. The form is always running, but set to hidden until I receive a hotkey event, at which time I set the visible property to true. The code looks like this:

void hook_volumeDown(object sender, KeyPressedEventArgs e)
{
    this.Visible = true;
}

It should be noted that the topmost property of this form is set to true.

The really odd part is, after my C# app has stolen focus from another application, it will never do it again. For example: I launch my app, then launch some fullscreep app like Team Fortress 2. Then I press my hotkey. Team Fortress 2 minimizes, and I see my form. Then, however, I can restore TF2, and press my hotkey again all I want (with the desired effect), and TF2 will remain focused.

At any rate, I'm looking for a way to fix this. I've found a lot of questions here covering similar problems, but all of them are related to creating/launching a new form, not making an existing one visible (unless I missed something). I could rework the application to create a new form every time I need one, but that would entail creating yet another form to be invisible all the time just to wait for hotkey events, so I'd rather leave it as it is.

Any ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms