PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] how do you add an event that will be used by several button clicks?-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] how do you add an event that will be used by several button clicks?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Resolved [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,
    Blake

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,621

    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.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    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!
    Blake

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,048

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    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
    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,
    Blake

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,048

    Re: [RESOLVED] how do you add an event that will be used by several button clicks?

    Quote Originally Posted by blakemckenna View Post
    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:
    1. TextBox1 = New TextBox
    2. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    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,
    Blake

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,048

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: [RESOLVED] how do you add an event that will be used by several button clicks?

    Thank you sir!
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width