Form's lost focus in C#

This may be a simple C# question but I need a solution.

I have two forms, form1 and form2, with form1 having a button. On the click of the button, I want to show form2. When form2 looses focus I want to hide it (form2). How can I do this? I tried to subscribe to the LostFocus event of form2, but it isn't working.

Please help me with this.

Note -- I use .Net 2.0

Upvotes: 18

Views: 24717

Answers (2)

Jcl
Jcl

Reputation: 28272

Use the Deactivate event handler

Upvotes: 35

Kent Boogaart
Kent Boogaart

Reputation: 178820

If I understand your question, I think you actually want to trap deactivation. Button handler inside your main form:

private void button1_Click(object sender, EventArgs e)
{
    Form childForm = new Form();
    childForm.Deactivate += delegate
    {
        childForm.Close();
    };

    childForm.Show();
}

Upvotes: 10

Related Questions