views:

52

answers:

2

I have a production application that I always want to force focus on. Sometimes the users will accidentally click off the form and the blue focused window will turn gray. I have a timer that fires every 30 seconds and I want to programatically give focus back to the form so that it goes from gray back to blue with focus. I've tried using the .focus event and the .activecontrol property but none of those seem to work and I'm not seeing any other viable options. Surely there is a way to do this, though right? I'm using Visual Studio 2008 in VB.Net.

Please advise.

+3  A: 

Nope, there isn't a way [anymore.] Even if you give focus to an application the best you can hope for is that its icon in the taskbar will flash three times. Thank christ they fixed this (since XP I think?), as while I see your point of view, for users this kind of behaviour is hellish. They should control their operating system and applications, not you. Sorry!

x0n
On top, even checking things like settings, explorer are painfull. Thank heavens misbehaving applications like that are blocked now by the OS. If you want tobe always in top, install your app as shell (supported, quite easy) do you exe replaces the complete windows UI after login.
TomTom
+1 for this answer. What happens when your program locks up, but won't let go of the focus?
Bill
I agree with you for normal desktop PCs. But there *are* valid purposes for this, for example in embedded PCs.
nikie
+1 [Raymond doesn't like it](http://blogs.msdn.com/b/oldnewthing/archive/2009/02/20/9435239.aspx)
MarkJ
I agree that most applications shouldn't do this behavior, but like I said, this is a production application for many non-tech savvy users who would only want to be on the window and it wouldn't be a hassle and every so often it causes problems when they lose focus.It's not a really big problem, so not being able to work around isn't the end of the world, but I did want to try to fix it if I could.
Jason Shoulders
+1  A: 

I have had good luck using the property, this.TopMost = true;

private void frmMain_Shown(object sender, EventArgs e)
{
// Make this form the active form and make it TopMost
this.ShowInTaskbar = false;
this.TopMost = true;
this.Focus();
this.BringToFront();
this.TopMost = false;
}

You can try making a function and calling it from your timer code. (Sorry it is in C#).

spinner_den
Thanks. I gave it a try but it didn't work for me :(
Jason Shoulders