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.
Related
This question already has answers here:
RoutedEventArgs.Source vs Sender
(4 answers)
How do C# Events work behind the scenes?
(2 answers)
Closed 5 years ago.
I don't understand what event parameters do in C#. Let's say we have a button called CoffeeButton, and clicking on it takes you to another Page called Coffee using a Frame called myFrame.
This is my code:
private void CoffeButton_Click(object sender, RoutedEventArgs e)
{
MyFrame.Navigate(typeof(Coffee));
}
What does object sender and RoutedEventArgs e do in this case?
Examples would be great!
Normally, "sender" will be a reference to whatever object fired the event. So if, for example, you have more than one Button that all wire into the same button_Click handler function, the sender object would be a reference to whichever actual Button object was clicked.
The EventArgs object that's normally passed in as the second parameter is used for different things depending on the context. Generally, it's used to pass you additional information related to the event that happened. For example, in this case, the RouteEventArgs object provides a RoutedEvent property that you can look at if you need to.
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;
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.SelectAll();
}
This works but I have 6 textBoxes. Is there any easier way instead of adding event listeners for each and every textbox? Or a shorthand or something?
Thanks.
Add the same event handler to each and have ((TextBox)sender).SelectAll() to ensure the one that is clicked on is highlighted.
If you're looking for something more generic create a derived class of TextBox containing the same.
In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form.
How do I do this?
I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:
private void button1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button!";
}
In the form designer, add an event handler to the button's Click event.
The form designer will give you a new method like this; add your code into this method:
private void button_Click(object sender, EventArgs e)
{
// Write some code that uses list boxes, labels, text boxes etc.
}
You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:
private void YourButton_Click(object sender, EventArgs e)
{
string someValue = yourTextBox.Text;
// do something with the value
}
If you want to add one event handler to many controls, you can do it.
Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.
But this method will be sutable if events compotable.
Describe your task more detail.
I have made a WinForms application with a custom richtextbox control.
I am referring it as a Windows control (a dll file) in my project. In my program for the richtextbox I have written functionality inside it's textchanged event.
I want to do additional work only after the textchanged event is fired or in other ways once the text is added to the textbox. Something like I want to call a function foo() in the text_changedevent. It only calls foo and fails to process the underlying textchanged event.
Any way in C# I can make it process the internal textchanged event first, and then look into my text changed event?
think of the scenario I have written the code for mytextbox_textchanged
private void txt_code_TextChanged(object sender, EventArgs e)
{
//some code which will always be called whenever textchanged event occurs.
}
Now I inherit this control in my project say MyApp1. Here I have a label where I want to display the number of lines contained inside my textbox. So I would write
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
//code to update the label with my_inherited_txt_code.lines.length
}
so my problem was, I first wanted the txt_code_TextChanged event to be called and then do the code written inside my_inherited_txt_code_TextChanged. Which was solved by writing
private void my_inherited_txt_code_TextChanged(object sender, EventArgs e)
{
base.OnTextChanged(e);
MessageBox.Show("foo");
}
Do you mean:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
// your code here...
}
I cant see why you shouldnt be able to call a method from the text_changed event?
Do you get any errors or what happens exactly?