tags:

views:

60

answers:

3

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.

+1  A: 

Have you tried the GotFocus event?

Justin Ethier
Yes, it raised only once when form is created.
Jacob Seleznev
+1  A: 

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

Ruben Bartelink
+2  A: 

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