By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Custom event handling for click of menu item.

P: n/a
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.

Feb 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <11**********************@l53g2000cwa.googlegroups .com>,
tr**************@yahoo.com says...
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.

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 22 '07 #2

P: n/a
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 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

Feb 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.