lozadakb
lozadakb

Reputation: 304

Check if Form has focus or is active

I have a Form that shows a Notification Window. But I want to show the popup only when the Form doesn't have focus or is not active, something like this:

if (!form.Active)
{
   //Do something
}

Is there a way to do it?

Upvotes: 12

Views: 18914

Answers (2)

vr_driver
vr_driver

Reputation: 2085

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.

    }
 }

Upvotes: 7

Vignesh Nethaji
Vignesh Nethaji

Reputation: 394

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

Upvotes: 15

Related Questions