Zeebok Zeebok - 20 days ago 7
C# Question

How to get form going to "Background state" Event in c#

Hope you all are fine. My question is, we have form_load event in c# forms. I want to do some job when my form1 will go in background and form2 will come to foreground. I have wasted my time in searching that, but could not found any help in this regard.
Your precious help even a little pointer can solve my issue. Thanks

Answer

Form.Activated Event

DOC

Occurs when the form is activated in code or by the user.

Form.Deactivate Event

DOC

Occurs when the form loses focus and is no longer the active form.

UPDATE to start and stop the timer:

class Form1: Form{
    void Form_Load(object sender, EventArgs arg)
    {
        this.Activated += form_Activate;
        this.Deactivate += form_Deactivate;
    }

    void form_Activate(object sender, EventArgs arg){
       timer.Start();
    }

    void form_Deactivate(object sender, EventArgs arg){
       timer.Stop();
    }
}
Comments