Solved

Need Handler for ToolStripMenuItem generated dynamic

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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