Results 1 to 3 of 3

Thread: EventArgs - How do I make use of the e.Equals() method?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile EventArgs - How do I make use of the e.Equals() method?

    Hi folks,

    I have several buttons on my form that will be calling the same method, but with different actions being performed inside this method depending on whichever button was clicked.

    What I am doing is passing into this method the EventArgs e object of each button and then trying to check this via e.Equals() to determine my course of action within the method, but I've had no luck so far. How should I go about getting this to work? I've tried the following, but it wont compile.

    Code:
    if(e.Equals(button1_Click()))
    {
    //Won't work. :(
    }
    
    if (sender.Equals(button4_Click()))
    {
    //I've tried passing in the sender object, but this doesn't work either.            
    }
    I searched the forums and did a google before posting - I saw mention of delegates and events with examples - but I'm not really any the wiser for it.
    A pointer in the right direction would be appreciated thanks.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: EventArgs - How do I make use of the e.Equals() method?

    You're going about this the wrong way. The 'e' argument contains data for the event itself. If the type of the 'e' argument is EventArgs then it contains nothing useful. EventArgs is the base class for all other event handler arguments and is used explicitly when the event doesn't require any data. The 'sender' argument IS the object that raised the event, so in your case the 'sender' argument will be a reference to the Button that was clicked.
    Code:
    if (sender == this.button4)
    {
        // button4 was clicked.
    }
    Having said that, what's the point of having a common event handler if you want to do something different for each Button? You would use a common event handler if you have common functionality. If you want to do something different for each Button then they should have individual event handlers. Remember that if you have SOME common functionality then you can put that into a method which you can then call from the Click event handler for each individual Button.
    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

  3. #3
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: EventArgs - How do I make use of the e.Equals() method?

    Maybe you can cast the sender to button

    Code:
    if (((Button)sender).Text == "btton1")
    				MessageBox.Show("Message");

Posting Permissions

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



Click Here to Expand Forum to Full Width