|
-
Apr 5th, 2006, 07:33 AM
#1
Thread Starter
Fanatic Member
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.
-
Apr 5th, 2006, 09:11 AM
#2
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.
-
Apr 5th, 2006, 09:22 AM
#3
Thread Starter
Fanatic Member
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.
-
Apr 5th, 2006, 05:06 PM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|