Solved

handling events for context menu items

Posted on 2010-11-16
2
741 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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