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();
       }
    }
}

why dont you mark this as solved?

I agree! :)

me too

sknake and DdoubleD agreed with me just to increase their solved thread numbers :)

Well, that and I noticed there were new posts that got my curiosity, so only seemed appropriate.:icon_cheesygrin:

I know this thread is kinda old, but there is an easier method to this.

this.LostFocus+=new EventHandler(this.FrmArea_LostFocus);
private void FrmArea_LostFocus(object sender, EventArgs e)
        {
            TopMost = true;
        }

Just thought I'd share.

This is not the same effect. It may seem similar. But it's not. Thanks for the input though.

This is great! Thanks.

I needed a stop method so I thought I'd share it.

public void Stop()
{
    t.Stop();
    form.TopMost = false;
    form.Activated -= form_Activated;
    form.Deactivate -= form_Deactivate;
}

Generally using a polling timer is to check for events like this is not a good idea. It will consume more CPU than using events or window messages and has a delay period in between each update. Also, even though your timer is set to a very fast firing 10ms (it will actually fire at closer to 30ms intervals FYI) this could cause some problems depending on the application. In this situation, though, it's a lot less complex than the alternative, which would be to hook CBT messages for your applications thread so I suppose it's acceptable.

This is a really solid example of how to hook CBT messages locally. You can also hook them globally (OS wide) but that becomes even more complex.

I'd be willing to test it out if someone wants to help me locate which hooks to use to keep a form activated even when the mouse is elsewhere (which is exactly what I need it to do as I have written a xpath type recorder for the windows ui tree).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.