On Feb 21, 8:07 am, trialproduct2.. .@yahoo.com wrote:
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 ToolStripMenuIt em
Protected Overrides Sub OnClick(ByVal e As System.EventArg s)
Dim myEvent As New MyMenuEventArgs
myEvent.MyCusto mProperty = "Custom Data"
MyBase.OnClick( myEvent)
End Sub
End Class
Public Class MyMenuEventArgs
Inherits System.EventArg s
Private _myCustomProper ty As String
Public Property MyCustomPropert y() As String
Get
Return _myCustomProper ty
End Get
Set(ByVal value As String)
_myCustomProper ty = 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(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Dim customMenuItem As New MyMenuItemClass ()
AddHandler customMenuItem. Click, AddressOf MenuItem_Click
RootMenu.DropDo wnItems.Add(cus tomMenuItem)
End Sub
Private Sub MenuItem_Click( ByVal sender As System.Object, ByVal e
As System.EventArg s)
If TypeOf sender Is MyMenuItemClass Then
MsgBox("Custom data for menu: " & DirectCast(e,
MyMenuEventArgs ).MyCustomPrope rty)
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