?
Solved

Need Handler for ToolStripMenuItem generated dynamic

Posted on 2014-07-30
3
Medium Priority
?
303 Views
Last Modified: 2014-07-31
I have a ContextMenuStrip that I am creating items for dynamically with info from SQL server

        If objDR.HasRows Then
            While objDR.Read()
                With reportsContextMenuStrip
                   .Items.Add(objDR.Item("ReportName"))
                    AddHandler .Click, AddressOf cMenu_Click

                End With
            End While
        End If
---------------------------------------
    Private Sub cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim mytext As String
        mytext = DirectCast(sender, ToolStripMenuItem).Text '  <<<<<<<<< Error Here
     
    End Sub

When I click It calls cMenu_Click       {"Unable to cast object of type 'System.Windows.Forms.ContextMenuStrip' to type 'System.Windows.Forms.ToolStripMenuItem'."}

I did try casting  as ContextMenuStrip but had no info to work with.
0
Comment
Question by:Charles Baldo
[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
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40230155
Maybe you can try this, instead of all the AddHandler calls for each item do it once on the parent:
AddHandler reportsContextMenuStrip.ItemClicked, AddressOf cMenu_Click

Open in new window

Then make this your handler:
    Private Sub cMenu_Click(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs)
        Dim mytext As String
        mytext = e.ClickedItem.Text
    End Sub

Open in new window

0
 

Author Comment

by:Charles Baldo
ID: 40231579
Robert,

Thanks I will check it out in a few hours
0
 

Author Closing Comment

by:Charles Baldo
ID: 40231581
getting the idea
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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