Custom event handling for click of menu item.

T

trialproduct2004

Hi all,

I am having application in whihc i am inserting menuitem dynamically.

When i add menuitem to mainmenu, i want to pass extra parameter to
eventargs. So what i did is derived class from eventargs and created
my own variable into this class.

And then while setting eventhandler i am passing this custom event
args. But this is not working.

Can anyone tell me what i am doing wrong. Can't i create my own
handler and attach it to client event.

Please help me asap.

Any sample code will be truely appreciated.
 
P

Patrick Steele

I am having application in whihc i am inserting menuitem dynamically.

When i add menuitem to mainmenu, i want to pass extra parameter to
eventargs. So what i did is derived class from eventargs and created
my own variable into this class.

The MenuItem class already defines a specific signature for each of its
events. You can't change that.

If you want to store "extra" information with your MenuItem's you could
either subclass it, or, if its something really simple, use the Tag
property.
 
C

Chris Dunaway

Hi all,

I am having application in whihc i am inserting menuitem dynamically.

When i add menuitem to mainmenu, i want to pass extra parameter to
eventargs. So what i did is derived class from eventargs and created
my own variable into this class.

And then while setting eventhandler i am passing this custom event
args. But this is not working.

Can anyone tell me what i am doing wrong. Can't i create my own
handler and attach it to client event.

Please help me asap.

Any sample code will be truely appreciated.

You would have to derive your own menu item and add your custom data
to it. Something like this (check syntax):

Public Class MyMenuItemClass
Inherits ToolStripMenuItem

Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
Dim myEvent As New MyMenuEventArgs
myEvent.MyCustomProperty = "Custom Data"

MyBase.OnClick(myEvent)
End Sub

End Class

Public Class MyMenuEventArgs
Inherits System.EventArgs

Private _myCustomProperty As String
Public Property MyCustomProperty() As String
Get
Return _myCustomProperty
End Get
Set(ByVal value As String)
_myCustomProperty = value
End Set
End Property
End Class

Then when you add your menu item dynamically and handle the click
event you can get the custom property data:

'In the Form class:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim customMenuItem As New MyMenuItemClass()
AddHandler customMenuItem.Click, AddressOf MenuItem_Click

RootMenu.DropDownItems.Add(customMenuItem)
End Sub


Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs)
If TypeOf sender Is MyMenuItemClass Then
MsgBox("Custom data for menu: " & DirectCast(e,
MyMenuEventArgs).MyCustomProperty)
Else
MsgBox("Normal menu item")
End If
End Sub

Notice in the MenuClick handler I first check to make sure the sender
is actually an instance of my custom menu class. If that is true,
then I can safely cast the EventArgs parameter (e) to my custom event
args.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top