Results 1 to 4 of 4

Thread: Add event handler to contextmenustrip item

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Add event handler to contextmenustrip item

    I am trying to add a menuitem and an event handler to a contextmenustrip :
    I can add an item easy enough with :
    Code:
    this.contextMenuStrip1.Items.Add("test choice");
    But if i try to add the item and a handler with :
    Code:
    this.contextMenuStrip1.Items.Add("test choice", new EventHandler(NewLoad));
    it throws a wobbler :-(

    I realise that it has something to do with delegates and so on but I'm pretty new to custom event handling.

    Can anyone point me in the right direction ???
    Thanks in Advance
    Last edited by venerable bede; Apr 5th, 2006 at 07:56 AM.

    Parksie

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

    Re: Add event handler to contextmenustrip item

    That's because there is no such overload. The only overload of Add that takes an event handler has three arguments: text, image and onClick. You've omitted the second argument.
    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

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Add event handler to contextmenustrip item

    You've lost me a bit.

    If I change it to this :
    Code:
    this.contextMenuStrip1.Items.Add("test choice", null, new EventHandler(NewLoad));
    I get the message :

    No overload for "NewLoad" matches delegate 'System.EventHanddler'

    I'm a bit confused.
    Last edited by venerable bede; Apr 5th, 2006 at 09:51 AM.

    Parksie

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

    Re: Add event handler to contextmenustrip item

    That means that your NewLoad method doesn't have the correct signature to handle the specified event. It must return no value and have two arguments. Remember the 'sender' and 'e' arguments that every event handler has? In this case they must be type Object and EventArgs respectively.

    Whenever I want to write a method that will be used as an event handler I let the IDE do it for me, so I know I'm getting the right signature. I'd create a dummy menu item and then double-click it to create the event handler. I'd then remove the menu item and the method created will remain. You can then rename that method if you desire and use it as an event handler for any events of the appropriate type, like menu items that you add dynamically as you are now.
    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

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