Solved

handling events for context menu items

Posted on 2010-11-16
2
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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