Jacob Seleznev
Jacob Seleznev

Reputation: 8131

Detect when application becomes active

In MDI application which event can the child Form class use to detect when the application becomes active?

I tried Form.Acivated event but it occurs only when the form is activated and doesn't when the application gets focus.

Upvotes: 1

Views: 414

Answers (3)

Hans Passant
Hans Passant

Reputation: 941455

It is the MDI parent form that gets the Activated event. You can subscribe to the event in your child form's Load event. Be careful, you have to make sure you unsubscribe the event when the child gets closed or you'll leak the child form instance. Make it look like this:

protected override void OnLoad(EventArgs e) {
  var main = this.MdiParent;
  main.Activated += main_AppActivated;
  this.FormClosed += (o, ea) => main.Activated -= main_AppActivated;
}

void main_AppActivated(object sender, EventArgs e) {
  // Etc...
}

Upvotes: 2

Ruben Bartelink
Ruben Bartelink

Reputation: 61795

While WPF has such a notion, WinForms does not to the best of my knowledge; you'd need to use Form-level events (like GotFocus from the earlier answer).

Upvotes: 1

Justin Ethier
Justin Ethier

Reputation: 134157

Have you tried the GotFocus event?

Upvotes: 1

Related Questions