If I have this action function for a button:
private: System::Void button1_Click(System.Object sender, System.EventArgs e)
{}
I am wondering about who will assign for e & sender their values?
The code that raises the event must set the sender and event args. For example, if the button in question is a Winforms element, then the values would be set by the System.Windows.Forms code, and you should be able to find the details for it in the MSDN documentation.
Generally, by convention, the sender will be the object instance that has raised the event. The event args will contain any other information that is relevant to the event.
Related
What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?
Scenario:
I'm trying to build a custom control with a delete function, and I want to be able to delete the control that was clicked on a page that contains many of the same custom control.
The sender is the control that the action is for (say OnClick, it's the button).
The EventArgs are arguments that the implementor of this event may find useful. With OnClick it contains nothing good, but in some events, like say in a GridView 'SelectedIndexChanged', it will contain the new index, or some other useful data.
What Chris is saying is you can do this:
protected void someButton_Click (object sender, EventArgs ea)
{
Button someButton = sender as Button;
if(someButton != null)
{
someButton.Text = "I was clicked!";
}
}
sender refers to the object that invoked the event that fired the event handler. This is useful if you have many objects using the same event handler.
EventArgs is something of a dummy base class. In and of itself it's more or less useless, but if you derive from it, you can add whatever data you need to pass to your event handlers.
When you implement your own events, use an EventHandler or EventHandler<T> as their type. This guarantees that you'll have exactly these two parameters for all your events (which is a good thing).
Manually cast the sender to the type of your custom control, and then use it to delete or disable etc. Eg, something like this:
private void myCustomControl_Click(object sender, EventArgs e)
{
((MyCustomControl)sender).DoWhatever();
}
The 'sender' is just the object that was actioned (eg clicked).
The event args is subclassed for more complex controls, eg a treeview, so that you can know more details about the event, eg exactly where they clicked.
'sender' is called object which has some action perform on some
control
'event' its having some information about control which has
some behavoiur and identity perform
by some user.when action will
generate by occuring for event add
it keep within array is called event
agrs
FYI, sender and e are not specific to ASP.NET or to C#. See Events (C# Programming Guide) and Events in Visual Basic.
This question already has answers here:
Pass extra parameters to an event handler?
(10 answers)
Closed 6 years ago.
I have the following:
private void ButtonClick(object sender, EventArgs e)
When I add to the parameter list, to make:
private void ButtonClick(object sender, EventArgs e, Class c)
then it will cause all sorts of problems. However, I need really want in my main:
Class c = new Class();
And then I would like to click a button so that ButtonClick is called, but I really need access to the "Class c" in the function body, so I really need
private void ButtonClick(object sender, EventArgs e, Class c)
to compile somehow.
I have tried other ways around the issue, such as making Class static, but it would create too much refactoring and cause other errors.
I don't know if this will be possible in Windows Forms. I am not opposed to switching over to XAML and WPF, I just want to know that doing something such as
private void ButtontClick(object sender, EventArgs e, Class c)
will be possible.
Just make your Class c variable a member variable in your main class where your ButtonClick event handler is located. Unless this ButtonClick event handler is your own custom event handler, you can't add more parameters to the framework's Click event handler for a button. You will have to access your Class c variable as a member variable or use commands and command bindings with command parameters.
You can put c in the Button.Tag property and use ((Button)sender).Tag in the click event handler to get it.
When I click on a button in e.g. a WinForms application, what information is passed to the EventArgs e of the event method? I'm just wondering because I'm using the as keyword to "convert" e to a mouse event in order to get the coordinates of the point where the button was clicked.
EDIT:
In the following example I can convert the variable e to an object of the type MouseEventArgs, and I want to know to what other types of event arguments e can be converted.
private void someEvent(object sender, EventArgs e)
{
int xCoord = (e as MouseEventArgs).X;
}
This depends on the event, The underlying windows messages for most winforms events have no such concept, so where ever the EventArgs were created will determine the type and the information it contains. It could be something from the framework or you can just make up your own class derived from EventArgs.
After .Net4.5 it doesn't even have to derive from EventArgs
You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."
There are 2 parameters: a sender, and an EventArgs. The sender is the object that initialized the event. The EventArgs contains additional information about the event.
From MSDN
// This example uses the Parent property and the Find method of Control to set
// properties on the parent control of a Button and its Form. The example assumes
// that a Button control named button1 is located within a GroupBox control. The
// example also assumes that the Click event of the Button control is connected to
// the event handler method defined in the example.
private void button1_Click(object sender, System.EventArgs e)
{
// Get the control the Button control is located in. In this case a GroupBox.
Control control = button1.Parent;
// Set the text and backcolor of the parent control.
control.Text = "My Groupbox";
control.BackColor = Color.Blue;
// Get the form that the Button control is contained within.
Form myForm = button1.FindForm();
// Set the text and color of the form containing the Button.
myForm.Text = "The Form of My Control";
myForm.BackColor = Color.Red;
}
I'm using the as keyword to "convert" e to a mouse event
Don't do it. Why?
Because if someone uses keyboard (TABs + Enter) or Button.PerformClick call to trigger your button, the conversion will fail and your:
MouseEventArgs mouseArgs = e as MouseEventArgs ;
if (mouseArgs.MouseButton == MouseButton.Left) ...
will lead to NullReferenceException.
You can't guarantee that EventArgs e in OnClick event will be MouseEventArgs. It is just the most common scenario, but not the only one possible.
P.S.: As it has been already pointed by #terribleProgrammer you could use Cursor.Position static property to get the current cursor position in more independent and robust way.
But if the cursor information makes sense only if the button has been triggered by mouse, then you will have just to be careful and handle possible conversion failure:
MouseEventArgs mouseArgs = e as MouseEventArgs ;
if (mouseArgs == null)
{
Logger.Log("mouseArgs is null");
return;
}
if (mouseArgs.MouseButton == MouseButton.Left) ...
EDIT:
There are three main ways to raise the event and three corresponding eventArgs classes:
Using mouse - MouseEventArgs
Using keyboard - KeyEventArgs
Using PerformClick method - I am not sure, but it will probably be just plain EventArgs(to verify just raise it in such a way and take a look at e.GetType()).
Whether there are any other scenarios that will raise it, I don't know. MSDN is of no use for such requests.
P.S.1.: And I want to repeat again - do not assume that e parameter will be of some specific type. Windows Forms API even does not guarantee that e will contain some concrete useful EventArgs-derived class.
When I manually cast the object sender and Eventargs e to a class like below what is it I am exactly doing? I know that it allows me to access all the arguments that have been passed and also to manipulate the object sender (as below):
private void Button1_Click(object sender, EventArgs e)
{
/ /casting the arguments
MouseEventArgs eventargs = e as MouseEventArgs;
Button button1 = sender as Button;
// displays which mouse button I used
MessageBox.Show(eventargs.Button.ToString());
// displays the name of the button I clicked
MessageBox.Show(button1.Name.ToString());
// changes the text of the button
button1.Text = "Ive changed";
}
I feel like I don't understand how this works, only that it works.
Also, it seems quite easy to write an event handler that serves several objects of the same type, but not one that can handle different types of event or different types of object ie:
private void Generic_Event_Handler(object sender, EventArgs e)
{
// displays what object and event triggered the handler
MessageBox.Show(sender.ToString());
MessageBox.Show(e.ToString());
}
Is this ever used? Is there a decent explanation of eventhandlers out there?
The following signature
private or protected void EventHandlersName (object sender, EventArgs e)
is the signature that they have all the event handlers in .NET. The parameter called sender is associated with the object on which the event called e was raised.
Why the type of sender is object?
Because all types in .NET has as their base type the type System.Object. So this way it doesn't make any difference if we have a click event on button on a win form, or in a WPF applciation or in an ASP.NET web form button.
Why we should manually cast the object sender?
We should do so, in order we have access to the properties and the methods of the specific type we have in each case.
For instance, all controls may not have a property called Name. Furthermore the base type called System.Object doesn't have. So if you don't cast the object sender to a Button class, then you can't read it's property called Name.
The signature of an event handler in .Net is (or at least should be):
(object sender, XXArgs e) where XXArgs is a class that inherits from EventArgs.
sender is, well, the sender of the event. In your example, if you click on a button, the button will fire the event using its own instance reference (this) for the sender parameter (so sender is a reference to your button). This is useful because you don't need to store references to the sender of the event ; it's available right here in the event handler.
For the XXArgs part, it contains information about the event. You shouldn't cast it actually, but write your handler with the right signature.
Basically, for a mouse click on a button, the right signature of the event handler would be:
private void Control_MouseClick(Object sender, MouseEventArgs e)
{
}
sender is a reference to type object. The actual object that it refers to could be anything; (in the specific case of an event handler, if everything is working as intended it should refer to the control that generated the event). When you say:
Button button1 = sender as Button;
you are trying to use a Button reference to refer to the same underlying object. If the underlying object is compatible (ie: is a Button), then the button1 reference will enable you to utilize the Button members of the underlying object. Otherwise, with the sender reference, you would only have been able to utilize the object members of the underlying object.
Note that it could still be something more "concrete" or specific than a Button, and then you would need a different cast to get at the more derived members.
Also you can cast page in case of protected void Page_Load(object sender, EventArgs e)
var senderInfo = (Page)sender;
Is there any standard way to route all Key events from the control A to other control B? I wish that the keyboard focus will still be on A however the event handler of A would trigger the all event handlers of B for the key events.
edit: Clarification: calling a specific event handler I wrote for B is not enough. I need to mimic the actual event. So for example I want that if a key is sent to a TextBox, it would be written to the TextBox. The solution given below does not do that (not to mention the fact that if new event handlers are added to B it completely fails).
I'm aware that WPF differentiates between logical focus and keyboard focus, but I need both focuses to remain on control A, but in a certain cases route its incoming event to other controls.
Couldn't you do something like this?
private void button1_Click(object sender, RoutedEventArgs e)
{
// Check if the event needs to be passed to button2's handler
if (conditionIsMet)
{
// Send the event to button2
button2.RaiseEvent(e);
}
else
{
// button1's "Click" code
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
// button2's "Click" code
}
Edit: Modified code to use the RaiseEvent() method to programmatically raise a specific event, rather than just calling the event handler for button2.