473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Understanding an event question GDI+ example

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

Nov 4 '06 #1
3 1644
"jm" <ne***********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
[...]
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).
No. It "sends" (passes as the second argument) a reference to an
instantiated PaintEventArgs class instance, which happens to contain a
reference to an instantiated Graphics class instance.
What happens to the objSender? Why does it just "disappear"?
What do you mean by "disappear"? It doesn't go anywhere. It just doesn't
happen to be used in this case. The "object" argument for the event handler
is often unused...it's provided for convenience in case the code does need
to use it, but it is not required to be used.
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.
This is the standard general purpose "pattern" for event handlers. The
first argument to the handler method is a reference to the object, and the
second is a reference to the EventArgs (either an EventArgs itself, or some
class derived from EventArgs, as the case here). In some cases, neither
argument is actually needed, but they are always provided so that the events
can follow a standardized structure.
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?
Because that's the derived type used specifically with the Paint event.
Why do I have to take a Graphics object, assign
it to pea,
There is no code assigning a Graphics object to the "pea" argument. Doing
that would make no sense.
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); ?
The argument "pea" has type PaintEventArgs. That type does not have a Clear
method, nor is there any reason for it to. Without a Clear method, the line
of code you suggest is illegal.

The PaintEventArgs class exists solely to contain data relevant specific to
the event in question. In this case, that's the Paint event, and so it
contains the generic things found in EventArgs, along with a reference to a
Graphics object, which is needed to actually draw.

You could just have written:

pea.Graphics.Clear(Color.Chocolate);

instead, but it's common practice to go ahead and store the Graphics
reference in a local variable so that the object can be referenced using
more concise code.

Pete
Nov 4 '06 #2
jm

Peter Duniho wrote:
"jm" <ne***********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
[...]
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).

No. It "sends" (passes as the second argument) a reference to an
instantiated PaintEventArgs class instance, which happens to contain a
reference to an instantiated Graphics class instance.
What happens to the objSender? Why does it just "disappear"?

What do you mean by "disappear"? It doesn't go anywhere. It just doesn't
happen to be used in this case. The "object" argument for the event handler
is often unused...it's provided for convenience in case the code does need
to use it, but it is not required to be used.
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.

This is the standard general purpose "pattern" for event handlers. The
first argument to the handler method is a reference to the object, and the
second is a reference to the EventArgs (either an EventArgs itself, or some
class derived from EventArgs, as the case here). In some cases, neither
argument is actually needed, but they are always provided so that the events
can follow a standardized structure.
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?

Because that's the derived type used specifically with the Paint event.
Why do I have to take a Graphics object, assign
it to pea,

There is no code assigning a Graphics object to the "pea" argument. Doing
that would make no sense.
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); ?

The argument "pea" has type PaintEventArgs. That type does not have a Clear
method, nor is there any reason for it to. Without a Clear method, the line
of code you suggest is illegal.

The PaintEventArgs class exists solely to contain data relevant specific to
the event in question. In this case, that's the Paint event, and so it
contains the generic things found in EventArgs, along with a reference to a
Graphics object, which is needed to actually draw.

You could just have written:

pea.Graphics.Clear(Color.Chocolate);

instead, but it's common practice to go ahead and store the Graphics
reference in a local variable so that the object can be referenced using
more concise code.

Pete
Thank you.

General OOP question about this, please. The PaintEventArgs class has
a reference to yet another class? I guess I've only seen simple
classes or not really thought about them containing references to other
classes. Does the Graphics class reference here mean anything
concerning inheritance? Graphics doesn't inherit anything from
PaintEventArgs or isn't related somehow that allows the reference in
the PaintEventArgs to the Graphics class or could there have been a
reference to any class (assuming I could use it here)?

Nov 6 '06 #3
"jm" <ne***********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
General OOP question about this, please. The PaintEventArgs class has
a reference to yet another class?
Yes. You can put any sort of data into a class that you want, including a
reference to another class instance. A reference to *any* class instance.
I guess I've only seen simple
classes or not really thought about them containing references to other
classes. Does the Graphics class reference here mean anything
concerning inheritance?
No, it doesn't relate to inheritance at all.
Graphics doesn't inherit anything from
PaintEventArgs or isn't related somehow that allows the reference in
the PaintEventArgs to the Graphics class or could there have been a
reference to any class (assuming I could use it here)?
The reference could have been anything. It just happens in this case that
it makes sense for the PaintEventArgs to contain a reference to an instance
of the Graphics class.

Pete
Nov 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
I'm trying to gain a better understanding of javascript by studying examples. I noticed this in an online tutorial. I don't get the use of "this". >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You might want...
4
by: Bob Rock | last post by:
Hello, is there a way to dynamically query if a callback delegate has been tied to an event to avoid raising the event if none have been tied or raise an exception if proper for the situation???...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
1
by: James dean | last post by:
Could someone explain how this works. I think the graphics card is used to do blitting and drawing shapes like rectangles. How does it draw using the Graphics card on the PC and why is this feature...
5
by: Don | last post by:
Is there a way to capture click events in the form designer? I'm trying to create a control similar to the TabControl (which seems to handle clicks in design mode) and would like to be able to...
7
by: hamil | last post by:
The following code will display a tif file on a form. When another form is moved over the tif image, the tif image is erased where the form was moved. A paint event occurs when this happens. My...
7
by: Marcin Rzeznicki | last post by:
Hello, Do you think it is legitimate practice to mix GDI+ and GDI calls (via Get/ReleaseHDC()) in paint event of a control? I've heard there is possibility of performance loss while "locking"...
4
by: Tom P. | last post by:
You wouldn't think this would be as hard as it is but for some reason I can't find a way to translate any of the codes in the KeyDownEventArgs into the actual characters they represent. The best...
8
by: pinkfloydfan | last post by:
Hi there I have a form with two TextBoxes each of which has an _Enter event. In each case the _Enter event binds to the same Excel SheetSelectionChange event. What I want to know is: within...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.