Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Form has focus or is active

Tags:

c#

winforms

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?

like image 412
lozadakb Avatar asked May 14 '18 14:05

lozadakb


People also ask

How to check if form is active in c#?

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. } }

How to check if control has focus c#?

To determine whether the control has focus, regardless of whether any of its child controls have focus, use the Focused property. To give a control the input focus, use the Focus or Select methods.

What form is active?

Active Form In active sentences, the thing doing the action is the subject of the sentence and the thing receiving the action is the object. Most sentences are active.


2 Answers

if (Form.ActiveForm != yourform)
{
   //form not active 
   //do something
}
else
{
   // form active
   // do something
}
like image 180
Vignesh Nethaji Avatar answered Sep 20 '22 16:09

Vignesh Nethaji


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.

    }
 }
like image 27
vr_driver Avatar answered Sep 21 '22 16:09

vr_driver