Solved

Need Handler for ToolStripMenuItem generated dynamic

Posted on 2014-07-30
3
290 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now