Solved

handling events for context menu items

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 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