EventHandler and sender object

  • Thread starter Thread starter Christoph Boget
  • Start date Start date
C

Christoph Boget

Please forgive my ignorance as I'm still new to developing w/C#
(and in windows in general). So if I use the wrong terms, please
bear with me.

I'm using VS.NET and defining the events have been really simple.
Just double-click on the event and the definition for that event (along
with the method) gets written. I see that the first argument that is
set up is "object sender" while the second argument varies with the
particular event. Now, what I can't quite figure out is what, exactly,
is passed in as the first argument? I've looked at what is stored in
the variable while debugging and it *seems* as if it's the object I,
say, double-clicked on. However, it's not really. For example, if
I double click on my TreeView control, I see that sender is:

{System.Windows.Forms.TreeView}

but when I try to use any TreeView method or access any TreeView
property, I get a build error saying

'object' does not contain a definition for ...'

Ok, fine. I understand why I got that error: object type is not the same
as TreeView type. So I'm wondering what, exactly, is passed as that
object? And how can I make practical use of it?

Thanks for your time and assistance!

Christoph
 
sender is whatever that fires the event. in your case, it is the TreeView. but to access TreeView specific members, you would have to cast it like ((TreeView)sender).TreeViewOnlyMembe

----- Christoph Boget wrote: ----

Please forgive my ignorance as I'm still new to developing w/C
(and in windows in general). So if I use the wrong terms, pleas
bear with me

I'm using VS.NET and defining the events have been really simple
Just double-click on the event and the definition for that event (alon
with the method) gets written. I see that the first argument that i
set up is "object sender" while the second argument varies with th
particular event. Now, what I can't quite figure out is what, exactly
is passed in as the first argument? I've looked at what is stored i
the variable while debugging and it *seems* as if it's the object I
say, double-clicked on. However, it's not really. For example, i
I double click on my TreeView control, I see that sender is

{System.Windows.Forms.TreeView

but when I try to use any TreeView method or access any TreeVie
property, I get a build error sayin

'object' does not contain a definition for ...

Ok, fine. I understand why I got that error: object type is not the sam
as TreeView type. So I'm wondering what, exactly, is passed as tha
object? And how can I make practical use of it

Thanks for your time and assistance

Christop
 
Hi.

The first argument is used to pass a reference to the same onject that is
calling the method, usally a "this" (but this is hidden to you , because
occurs inside the control you are handling).
You need to understand that the event handler you are writing is a method of
class, but the code you r writing needs to manipulate properties and members
of another class (i.e the control or componet you are using). So you need a
reference to that object at that very moment.
You can use the second argument to get some properties, values and
references that the control or component will pass into the method you are
writing.
It is a very complicated mechanism to understand and you need to read it
many times.

Best regards, Alejandro Penate-Diaz
 
As you figured out, you get an object representing the object the event was
raised from.

If you want to use the sender object, you have to cast it to apropriate
type.

private void button1_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if(b != null)
{
b.Text = "You clicked me";
}
}

Since you get an object as the sender, you can potentially use the same
event handler from different types of objects.
E.g Clicking a Button or MenuItem results in the same event.

Another option that is available is to use the same eventhandler for several
buttons. Each button can identify itself
using Name or Tag property.

Chris
 
Back
Top