Solved

handling events for context menu items

Posted on 2010-11-16
2
739 Views
Last Modified: 2012-05-10
I add menu items to my context menu strip, and then add a hanler for the click event.  In the subroutine, how can I tell which menu item was clicked?

I'm familiar with the static context menu strip where I can use "sender.tostring", but I don't think that will work for my runtime context menu strip.

In the form_load subroutine:

For each cgems in listArray
  Dim Y As New ToolStripMenuItem
  Y.Text = cgems
  Y.CheckOnClick = True
  Y.DisplayStyle = ToolStripItemDisplayStyle.Text
  AddHandler Y.Click, AddressOf Me.clickCGEMS
  cmsCgems.Items.Add(Y)
Next

0
Comment
Question by:motokent
2 Comments
 
LVL 13

Accepted Solution

by:
AngryBinary earned 500 total points
ID: 34149984
You can still use the "sender" property to determine which menu item was clicked on. You can use the "Text" property to differentiate menu items assuming they all have different "Text" values, though you may want to look into the "ToolStripMenuItem.Tag" property:

Protected Sub clickCGEMS (sender as Object, e as EventArgs)
    Dim item as ToolStripMenuItem = CType(sender, ToolStripMenuItem)
    If item.Text = "Menu option 1" Then
         'Do some stuff...
    Else If item.Text = "Menu option 2" Then
         'Do some other stuff...
    End If
End Sub

If you are binding that click handler to objects other than menu items, you will want to check their type before doing the CType cast:

Protected Sub clickCGEMS (sender as Object, e as EventArgs)
    If TypeOf(sender) is ToolStripMenuItem Then
        Dim item as ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        'Do some stuff...
    Else If TypeOf(sender) is Button Then
        Dim button as Button = CType(sender, Button)
        'Do some other stuff
    End If
End Sub




0
 

Author Closing Comment

by:motokent
ID: 34153749
Thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now