Solved

Need Handler for ToolStripMenuItem generated dynamic

Posted on 2014-07-30
3
291 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:charlesbaldo
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 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:charlesbaldo
ID: 40231579
Robert,

Thanks I will check it out in a few hours
0
 

Author Closing Comment

by:charlesbaldo
ID: 40231581
getting the idea
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 61
Get hold of longitude and latitude in iframe string 11 45
REXEX help Part 2 2 23
Expression Evaluater 3 24
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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 …

770 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