Event Handler Help - Passing of additional parameter

  • Thread starter Thread starter Vinay
  • Start date Start date
V

Vinay

Hi,

I have created a Context Menu class (CMenu) with relevant
items. Based on a selected Item, I am taking a separate
action. The following code is working fine.

CMenu(sOption as string)
....
TM = New ContextMenu

TM.MenuItems.Add("Case 1", AddressOf mItems)
TM.MenuItems.Add("Case 2", AddressOf mItems)
....

Private Sub mItems(ByVal sender As Object, ByVal e As
EventArgs)

Dim iClicked as new MenuItem

iClicked = CType(sender, MenuItem)

Select Case iClicked.Text

Case "1"
Msgbox iClicked.Text & "1"
Case "2"
Msgbox iClicked.Text & "2"

......
End Select
....
End Sub

Now I want to use an external parameter (string 'sOption'
passed to the class CMenu) in the sub mItems(). How can I
pass this parameter to this sub, as Addressof mItems
(sOption) is throwing an error. I don't want to use a
global variable.

Regards

Vinay
 
Hi,

Thanks for your input.

I understand your point about derived class, but I am not
able to ascertain how to override MenuItem.Add function
of base class. Can you Please elaborate with a code
snippet.

Regards

Vinay
 
You could also pass a parameter in your constructor as well, and then when
you do your add handler you have a reference...
 
Here are some sample code to achieve this:

Public Class CMenu
Inherits System.Windows.Forms.ContextMenu

Private MyMenuItemCollection As New
CMenuItemCollection(MyBase.GetContextMenu)


Shadows ReadOnly Property MenuItems() As CMenuItemCollection
Get

Return MyMenuItemCollection
End Get

End Property

End Class

Public Class CMenuItemCollection
Inherits System.Windows.Forms.Menu.MenuItemCollection

Public Sub New(ByVal o As System.Windows.Forms.Menu)
MyBase.New(o)
End Sub

Public msgCollection As New
System.Collections.Specialized.NameValueCollection

Public Overridable Overloads Function Add(ByVal caption As String,
ByVal onClick As EventHandler, ByVal msg As String) As MenuItem

MyBase.Add(caption, onClick)
msgCollection.Add(caption, msg)
End Function
End Class


Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top