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 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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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
|
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:
|
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...
| |
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
|
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 ?
|
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 ?",
|
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.
}
}
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |