C# form Activated and Deactivate events

18,326

Solution 1

This works on my machine.

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private Form2 _form2;
    private void Form1_Load(object sender, EventArgs e) {
        _form2 = new Form2();
        _form2.Show();
        HandleFocusEvents();
    }

    private void HandleFocusEvents() {
        this.LostFocus += Form_LostFocus;
        _form2.LostFocus += Form_LostFocus;
        this.GotFocus += Form_GotFocus;
    }

    private void Form_LostFocus(object sender, EventArgs e) {
        if (!_form2.ContainsFocus && !this.ContainsFocus) {
            _form2.Hide();
        }
    }

    private void Form_GotFocus(object sender, EventArgs e) {
        if (!_form2.Visible) {
            _form2.Show();
        }
    }
}

Solution 2

In your main forms code, where you create an new instance of the sub form, add an event that is fired whenever the instance of the sub form form is activated. In the event handler for it set a bool variable to true. Now, do the same, for the deactivate event of the sub forms instance, except set the bool variable to false. Now in the event for the main form loosing focus, before hiding it check that bool variable and make sure it is false "the sub form doesn't have focus" and only then would you hide the main form. I could provide code if I could see what you have so far. There are a lot of different ways you could to this. Hope this helps you!

Share:
18,326
goodbyeworld
Author by

goodbyeworld

Updated on June 04, 2022

Comments

  • goodbyeworld
    goodbyeworld almost 2 years

    I have two forms, mainForm and subForm. When mainForm loses focus I want subForm to disappear and then reappear as mainForm regains focus. I'm using the Activated and Deactivate events on the mainForm to keep track of whether mainForm has focus or not. When the Activated is fired I do subForm.Show() and the opposite for Deactivate. The problem I have is that when subForm gains focus mainForm disappear because I don't know how to say programmatically "make subForm disappear when the mainForm's Deactivate event fires except if it's because the subForm gained focus. The whole point of what I'm doing is to make both windows disappear when the mainForm loses focus because the user clicked on another application or use ALT+TAB to switch. I don't want to leave the subForm behind. Is there any way of checking as the Deactive fires whether it was because another form belonging to the application gained focus as opposed to some other application?

    class MainForm : Form
    {
        SubForm subForm = new SubForm();
    
        private void mainForm_Activated(object sender, EventArgs e)
        {
            this.subForm.Show();
        }
    
        private void mainForm_Deactivate(object sender, EventArgs e)
        {
            this.subForm.Hide()
    
            // I need some logic to make sure that it is only hidden
            // when the mainForm loses focus because the user clicked
            // some other application in the taskbar and not when the
            // subForm itself gains the focus.
        }
    }
    
  • goodbyeworld
    goodbyeworld over 11 years
    I don't want it to be a MDI application.
  • goodbyeworld
    goodbyeworld over 11 years
    It doesn't seem to work. I think the Deactivate of the mainForm gets fired before the Activated of the subForm.
  • FrostyFire
    FrostyFire over 11 years
    Oh! That makes sense. Howabout having your main form wait a while before checking. You could put each form on its own thread and pause the main one's thread for just a couple of ms to let the sub form activate. Unfortunately I'm still learning and threading is beyond me. Try googling "C# threading two form separately" Sorry I can't be of anymore help to you!