How do I add a "click" handler to a derived MenuItem class?

R

Robin Tucker

Hi there,

I need to add a click handler to a derived menu item class. I am using the
same handler to handle all of the items in the given menu, but need to
derive a new menu item class in order to store a little extra information
for each item.

Previously, I was writing:

MyMenu.MenuItems.Add ( "the text label", New EventHandler(AddressOf
MyMenuItem_Clicked)

where MyMenu_Clicked is the handler to use. Now I want to write:

Dim theItem as new MyMenuItemClass("the text label")

MyMenu.MenuItems.Add ( theItem )

...... --> somehow add the event handler MyMenuItem_Clicked to be the same
for each item in MyMenu.

This is a dynamic menu, so I can't hard-code each one. I notice there isn't
a MenuItems.Add method for MenuItem, EventHandler, although there is one for
String, EventHandler.

Thanks for any help you can give.
 
A

Armin Zingler

Robin Tucker said:
MyMenu.MenuItems.Add ( "the text label", New
EventHandler(AddressOf MyMenuItem_Clicked)

where MyMenu_Clicked is the handler to use. Now I want to write:

Dim theItem as new MyMenuItemClass("the text label")

Add another constructor to your MyMenuItemClass. The 2nd arg takes the event
handler. Call the base class constructor that also takes the event handler.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
H

Herfried K. Wagner [MVP]

* "Robin Tucker said:
I need to add a click handler to a derived menu item class. I am using the
same handler to handle all of the items in the given menu, but need to
derive a new menu item class in order to store a little extra information
for each item.

Previously, I was writing:

MyMenu.MenuItems.Add ( "the text label", New EventHandler(AddressOf
MyMenuItem_Clicked)

where MyMenu_Clicked is the handler to use. Now I want to write:

Dim theItem as new MyMenuItemClass("the text label")

MyMenu.MenuItems.Add ( theItem )

..... --> somehow add the event handler MyMenuItem_Clicked to be the same
for each item in MyMenu.

This is a dynamic menu, so I can't hard-code each one. I notice there isn't
a MenuItems.Add method for MenuItem, EventHandler, although there is one for
String, EventHandler.

Ctors don't get inherited, so you will have to redefine the ctors you
need (or all of them) and call the base class's ctors inside the ctors
defined in the derived class:

\\\
Public Sub New(...)
MyBase.New(...)
...
End Sub
///
 
R

Robin Tucker

Well what I did in the end was this:



Private Sub AddMenuItem(ByVal theText As String, ByVal theData As
MyDataClass)

Dim theNewItem As MyMenuItemWithDataClass = New MyMenuItemWithDataClass
(theText, theData )

AddHandler theNewItem.Click, AddressOf MyMenuItemWithData_Clicked

MyMenu.MenuItems.Add(theNewItem)
End Sub
 

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