Check if Form has focus or is active

if (Form.ActiveForm != yourform)
{
   //form not active 
   //do something
}
else
{
   // form active
   // do something
}

This may help you on your quest. If your form is active, it'll tell you. If you click off the form, it'll tell you too.

using System; 
using System.Text;          // probably not required
using System.Windows.Forms; // probably not required
using System.Threading;     // probably not required   


namespace AppName
{   

    public partial class Form1 : Form
    {
        
        protected override void OnActivated(EventArgs e)
        {
            Console.WriteLine("Form activated");
        }


        protected override void OnDeactivate(EventArgs e)
        {
            Console.WriteLine("Form deactivated");
        }

       // more program etc.

    }
 }

Tags:

C#

Winforms