[RESOLVED] how do you add an event that will be used by several button clicks?
I know how to do this in VB.Net but not sure how to accomplish it in C#. I basically need an event that will handle the click event for several buttons. Being new to C#, I'm just not sure how to construct it.
Thanks,
Re: how do you add an event that will be used by several button clicks?
Are you talking dynamically, or in the IDE?
In the IDE you can do it similarly to what you could do in VB.
As an example, if I put four buttons on a form, select all four, and then in the properties window, select the "lightening bolt" icon to get the list of events, scroll down to the one I want and then doubleclick on it, it will choose the name of one of the buttons and create an event handler for it. It will also assign the same sub to the same event for all the other controls selected.
If you don't want one of the button names to be used for all the buttons, you can change the name of the method to what you want, and you should be prompted by the IDE to change all the references to the old name to the new name, if you want.
If you want to add another button and use the same event handler, then copy the method name into the slot for that event in your Properties event list.
If you're talking about dynamically in code, then I would have to look that up, but you've probably already looked it up by now since it has been a few hours since you posted.
Re: how do you add an event that will be used by several button clicks?
passel,
I actually figured it out by finding some examples online. Your solution is what I found in the example. At least I'm on the right track.
Thanks!
Re: [RESOLVED] how do you add an event that will be used by several button clicks?
To be precise, you're talking about an event handler, not an event. In C#, there is no equivalent to the WithEvents/Handles combination in VB. You use '+=' to register a method as an event handler in C# in much the same way as you use AddHandler in VB.
Selecting multiple controls and then using the Properties window to add a common event handler works the same way in C# as it does in VB from the user's perspective. The different is that VB adds multiple controls to a single Handles clause while C# uses '+=' to add the same method to the event for multiple controls. If VB used AddHandler then it would be exactly the same.
Re: [RESOLVED] how do you add an event that will be used by several button clicks?
jmc,
You got me wondering about this because the current issue I'm having may have to do with something that I'm NOT doing. Regarding what you said
Quote:
Selecting multiple controls and then using the Properties window to add a common event handler works the same way in C#
I've done this in VB.Net so that's nothing new, however, C#'s equivalent to VB.Net's WithEvents is the "+=". Do I still need to do this along with what's in the quote?
Thanks,
Re: [RESOLVED] how do you add an event that will be used by several button clicks?
Quote:
Originally Posted by
blakemckenna
C#'s equivalent to VB.Net's WithEvents is the "+=".
It's not. C# doesn't have an equivalent to WithEvents. In VB, a field declared WithEvents can be used in a Handles clause. VB does some magic in that case so that the event handler follows the field rather than the object. For example, let's say that you add a TextBox to your form in the designer and double-click it to create a TextChanged event handler. You'll have a TextBox1 field declared WithEvents and you'll have 'Handles TextBox1.TextChanged' on the generated method. If you then execute this code at run time:
vb.net Code:
TextBox1 = New TextBox
Controls.Add(TextBox1)
You'll find that a new TextBox is added to your form and its TextChanged event will be handled by that method, while the existing TextBox will remain unchanged except that its TextChanged event will no longer be handled. The method handles the event specified in the Handles clause at any particular time, even if the object that it refers to changes. That is in contrast to the AddHandler statement, which registers an event handler for the specified object and that doesn't change regardless of what object gets assigned to what variable. The C# '+=' and '-=' operators work exactly like AddHandler and RemoveHandler and there is no C# equivalent of WithEvents/Handles. This:
Code:
AddHandler someObject.SomeEvent, AddressOf SomeMethod
is equivalent to this:
Code:
someObject.SomeEvent += SomeMethod;
Note that VB uses the AddressOf keyword to create a delegate because VB methods can be called without parentheses, whereas C# uses a method group, i.e. a method name without parentheses, because method calls require parentheses.
Re: [RESOLVED] how do you add an event that will be used by several button clicks?
jmc,
Does the below code look correct? Do I even need it since I assigned the method, which is "mnuButton_Click", that handles all the buttons in the Properties Event window?
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.Click += mnuButton_Click;
Button2.Click += mnuButton_Click;
Button3.Click += mnuButton_Click;
Button4.Click += mnuButton_Click;
Button5.Click += mnuButton_Click;
Button6.Click += mnuButton_Click;
Button7.Click += mnuButton_Click;
Button8.Click += mnuButton_Click;
}
}
Thanks,
Re: [RESOLVED] how do you add an event that will be used by several button clicks?
Yes, it looks right and no, you shouldn't need it if you have added the event handler via the designer.
Re: [RESOLVED] how do you add an event that will be used by several button clicks?