简体   繁体   中英

Event fired when any child inside a form changes (is painted)?

I'm pretty new to C#, Winforms and the Compact Framework and still didn't wrap my mind around it's event system completely.

I'm looking for a way to add a single event handler to my topmost form, that's called when any of the childs is (re)drawn (or even better, after they're drawn), no matter what type they are. It's also important that I don't need to add any code to the children's themself.

In pseudocode it would could look like this

TopMostForm {
    anyChildWasReDrawn() {
      dostuff();
    }
}

Is there any way to do this?

Look into handling the Paint handler of your children. For example, in your top most Form you would do something like this:

// each child will call the same method when any of them are redrawn
myChild1.Paint += myChildren_Paint;
myChild2.Paint += myChildren_Paint;
mySomeOtherChild.Paint += myChildren_Paint

The actual handler itself would look something like this:

void myChildren_Paint(object sender, PaintEventArgs e)
{
    dostuff();
}

Note that each child points to this one handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM