A couple of months ago I had wanted to keep a form active as long as it was visible, I couldn't find any help with this, a couple of forums led me into the dirt, as everything they suggested failed. I decided against the feature, but not before I found a way to do it.
seems there must be a delay between calling activate and deactivate on a form in order for it to work. and set foreground window no longer works the way it sounds. some suggested a timer to constantly activate the form, and that was buggy. but I found a way, by creating a separate class that activates the form after an interval from when the form was deactivated... works great,
I am not saying that it is ever a good idea to force a form to keep focus, but everything has it purpose. So here for your pleasure is an class from my forms extensions library, HoldFocus.
simply instantiate it,passing to it the form that should keep focus, keep in mind, this is annoying, but helpful in kiosk apps and the like, this class could easily be extended to hold a property that enables/disables the holding of focus, but that's another story.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DDFormsExtentions
{
class holdFocus
{
System.Windows.Forms.Form form;
Timer t;
public holdFocus(System.Windows.Forms.Form f)
{
form = f;
form.TopMost = true;
t = new Timer();
t.Interval = 10;
t.Tick +=new EventHandler(t_Tick);
form.Activated +=new EventHandler(form_Activated);
form.Deactivate +=new EventHandler(form_Deactivate);
}
private void t_Tick(object sender, EventArgs e)
{
form.Activate();
}
private void form_Activated(object sender, EventArgs e)
{
t.Stop();
}
private void form_Deactivate(object sender, EventArgs e)
{
t.Start();
}
}
}