>From a C. Petzold book I have Programming Windows C#. It's a little
old now, but anyway, it has on page 71 (shortened):
form.Paint += new PaintEventHandler(MyPaintHandler);
static void MyPaintHandler(object objSender, PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(Color.Chocolate);
}
When the paint event occurs the MyPaintHandler fires. In there, and
now I am guessing, the .NET framework sends the object that causes the
event (form here) as the first argument in MyPaintHandler (objSender),
then it sends an instantiated Graphics class (pea).
What happens to the objSender? Why does it just "disappear"? This
signature in .NET events has always confused me. What happens to that
object objSender sent to the event handler? No code there, so I don't
get it.
A specific GDI+ question, but it is related because there is always in
the events some kind of xxxxEventArgs in my applications. I know how
to use them, but I don't always understand why I need them. I just
mindlessly follow what other snippets do. Anyway, why do I need the
PaintEventArgs here? Why do I have to take a Graphics object, assign
it to pea, assign that to PaintEventArgs and then turn around and say:
Graphics grfx = pea.Graphics;
so that I can call:
grfx.Clear(Color.Chocolate);
Why couldn't I just have said:
pea.Clear(Color.Chocolate); ?
Thank you for helping me understand why things work, instead of just
what works.