473,783 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 PaintEventHandl er(MyPaintHandl er);

static void MyPaintHandler( object objSender, PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(Colo r.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(Colo r.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 1666
"jm" <ne***********@ gmail.comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.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(Colo r.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.Cl ear(Color.Choco late);

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******** *************@b 28g2000cwb.goog legroups.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(Colo r.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.Cl ear(Color.Choco late);

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.goo glegroups.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
1461
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 to add some event handler for an event to an element to which there is already an event handler attached. That is possible by storing the old event handler somewhere and then writing a new function which calls the old event handler.
4
2198
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??? Thx. Bob Rock
3
4266
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
1
5330
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 left out of GDI+?. *** Sent via Developersdex http://www.developersdex.com ***
5
4183
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 click on parts of the control in design mode and have the control react, but I can't seem to trap the events in design mode. - Don
7
1830
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 question is..how would I save the tif images as soon as it is initially painted, and then restore the image in a paint event. It seems to me that there should be some sort of a SaveImage method and a RestoreImage method that would be fast. Note:...
7
5976
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" Graphics object which is done as a side-effect to GetHDC() call - could you confirm? Another question that comes to my mind when planning mentioned operation is: if the control painted on uses double-buffering style, will GDI calls make use of "back...
4
11491
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 I can do is get the uppercase character using Convert.ToChar() but if the user types a lowercase character I still end up with the uppercase character. What I need to know is how do I get the correct character during the KeyDown event?
8
1891
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 the SheetSelectionChange event how can I determine which _Enter event fired it and therefore which TextBox I should be manipulating by this code?
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.