...(object sender, System.EventArgs e)
-
This is a really easy question i hope....... for example you have the following code; private void Form1_Load**(object sender, System.EventArgs e)** i would like to know what the bold part means?? im not too concerened about Form1_Load event, its just the parameters of any event. I cant seem to understand:mad: I have tried looking in books but had no success. Please help
Cheers :)
-
This is a really easy question i hope....... for example you have the following code; private void Form1_Load**(object sender, System.EventArgs e)** i would like to know what the bold part means?? im not too concerened about Form1_Load event, its just the parameters of any event. I cant seem to understand:mad: I have tried looking in books but had no success. Please help
Cheers :)
hustler2005 wrote:
I have tried looking in books but had no success.
Don't understand why. There is plenty of documentation, and you couldn't find anything? sender is rather obvious, the sender of the event. EventArgs is the data generated from the event
only two letters away from being an asset
-
hustler2005 wrote:
I have tried looking in books but had no success.
Don't understand why. There is plenty of documentation, and you couldn't find anything? sender is rather obvious, the sender of the event. EventArgs is the data generated from the event
only two letters away from being an asset
sorry, what i meant is "i have had no success" in understanding what i have read. if someone could explain it a lil more clear/easier to understand?? it would make my day! :-D
Cheers :)
-
This is a really easy question i hope....... for example you have the following code; private void Form1_Load**(object sender, System.EventArgs e)** i would like to know what the bold part means?? im not too concerened about Form1_Load event, its just the parameters of any event. I cant seem to understand:mad: I have tried looking in books but had no success. Please help
Cheers :)
As Mark said, there is already a lot of information about this online. Anyway, the
sender
parameter is an object representation of the control that caused the event to be sent. It is anobject
, which means that you need to have some knowledge of what the actual sender is supposed to be if you want to use it inside the event handler. In order to use thesender
you need to cast it to the appropriate type. In the case of your example, you would need to do one of the following:private void Form1_Load(object sender, System.EventArgs e)
{
Form f = sender as Form;
if (f != null)
{
// do something here
}
}or
private void Form1_Load(object sender, System.EventArgs e)
{
Form f = (Form)sender;
// do something here
}To me, the preferred way is the first one since it is a bit safer. The
e
parameter represents the "EventArgs" of the event. In reality,EventArgs
is a class that is used to hold the data generated by the event that could be used within the event handler. All events should use either theSystem.EventArgs
class or a custom (derived)System.EventArgs
class. The defaultSystem.EventArgs
class maintains no data about the event.Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
As Mark said, there is already a lot of information about this online. Anyway, the
sender
parameter is an object representation of the control that caused the event to be sent. It is anobject
, which means that you need to have some knowledge of what the actual sender is supposed to be if you want to use it inside the event handler. In order to use thesender
you need to cast it to the appropriate type. In the case of your example, you would need to do one of the following:private void Form1_Load(object sender, System.EventArgs e)
{
Form f = sender as Form;
if (f != null)
{
// do something here
}
}or
private void Form1_Load(object sender, System.EventArgs e)
{
Form f = (Form)sender;
// do something here
}To me, the preferred way is the first one since it is a bit safer. The
e
parameter represents the "EventArgs" of the event. In reality,EventArgs
is a class that is used to hold the data generated by the event that could be used within the event handler. All events should use either theSystem.EventArgs
class or a custom (derived)System.EventArgs
class. The defaultSystem.EventArgs
class maintains no data about the event.Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
Thanks Scott/Matt for your input. You mention there is alot of documentation online, can you suggest a good starting point?? This would be very help indeed.
Cheers :)
-
Thanks Scott/Matt for your input. You mention there is alot of documentation online, can you suggest a good starting point?? This would be very help indeed.
Cheers :)
Sure, a good starting point would probably be MSDN. Here are some links: EventArgs Class (System)[^] EventHandler Delegate (System)[^] Events and Delegates[^] Consuming Events[^]
Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]