473,594 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EventHandler and sender object

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
Nov 16 '05 #1
3 15237

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)send er).TreeViewOnl yMembe

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

Nov 16 '05 #2
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(o bject 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

"Christoph Boget" <jc*****@yahoo. com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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

Nov 16 '05 #3
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

"Christoph Boget" <jc*****@yahoo. com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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

Nov 16 '05 #4

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

Similar topics

1
1302
by: Al | last post by:
hi , I am creating a form in asp.net and in the form, I crateed a couple of buttons, when I call a function I get the following error : Method 'Public Sub (function name) does not have the same signature as delegate ' Delegate Sub EventHandler(sender as object, e as system.eventArgs)' I figured out that I must put the following at my funcion sub calcualte_int(source as object, e as EventArgs) .. process
3
6015
by: Marty | last post by:
Hi, I have this event that is performed by three different controls, how can I know (from sender object) wich control is used to trigger the event? Private Sub HighlightSelectedRow(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbxAmount.Click, lblID.Click, lblExpense.Click Dim ctlControl As System.Type ctlControl = sender.GetType
6
7140
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open in design view. This happens once I restarted Visual Studio adn opened teh solution Any ideas on how to fix this would be appreciated.. BR ::Ben
8
2067
by: Steven Blair | last post by:
Hi, dgTransaction_SelectedIndexChanged(object sender, System.EventArgs e) That sender is actually the object that raised the event. So, in this case you could do:
13
1646
by: Carl Howarth | last post by:
Hello there, I am currently in the process of 'getting strict' with some of our aspx.vb files. I have a function that is called either when a datarepeater is bound or when a button on the datareader is selected. The function accepts a 'sender' object, which may be of a different type depending on how the function is ran. In one instance the type is "System.Web.UI.WebControls.RepeaterItem", and on others it is...
2
6853
by: nomenklatura | last post by:
Hi, i call Call treeviewXi_DoubleClick("Try", e) from another procedur.. and in treeviewX_DoubleClick i use this string and do what i want example : if sender="try" then doit
2
1451
by: romy | last post by:
Hello dot.net winforms How Do I make casting from the sender object to controls in the form that called the sub ?
2
2915
by: Kalim Julia | last post by:
I need to delegate function using addhandler. AddHandler mlcmdDELETERECORD.Click, AddressOf DeleteGridRecordEvent Public Overridable Function DeleteGridRecordEvent(ByVal sender As Object, ByVal e As System.EventArgs) As Boolean mlDELETERECORDALLOWED = MsgBox("Are you sure want to delete the record ?",
10
5947
by: moondaddy | last post by:
in a wpf c# windows app, how do I determin if a control is a texbox? void Test(object sender) { if(sender==Texbox) { // do something. } }
0
7947
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
8255
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
8242
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
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...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5413
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
3868
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...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1217
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.