Solved

VB NET 2003 MenuItem Dynamic Creation.  How do I tell which item I clicked on ?

Posted on 2007-03-22
1
366 Views
Last Modified: 2012-06-27
Here is my code for my menu Item Dynamic Array.

Private Sub ButtonArray_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'On Error Resume Next
        Dim btn As Button = CType(sender, Button)

        MsgBox("The Button " & btn.Name & " Was clicked")
    End Sub



    Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
        If e.Button = MouseButtons.Right Then
            Dim myCounter As Integer = 0
            strFill = ""
            For myCounter = 0 To Me.DS_TAX_Business.Tables("Business").Rows.Count - 1
                strFill += Me.DS_TAX_Business.Tables("Business").Rows(myCounter)("Name") & "."
            Next
            strFill = Mid(strFill, 1, Len(strFill) - 1)

            Dim mnuItems() As MenuItem
            Dim mnuContext As ContextMenu
            Dim arrContext As Array
            mnuContext = New ContextMenu
            arrContext = strFill.Split(".")
            Dim i As Integer
            ReDim mnuItems(arrContext.GetUpperBound(0))
            For i = 0 To arrContext.GetUpperBound(0)
                mnuItems(i) = New MenuItem
                mnuItems(i).Text = arrContext(i)
                mnuContext.MenuItems.Add(mnuItems(i))
                AddHandler mnuItems(i).Click, AddressOf ButtonArray_Click
            Next
            mnuContext.Show(sender, New Point(e.X, e.Y))

        End If
    End Sub
0
Comment
Question by:mphillip85
[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
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18775947
"How do I tell which item I clicked on ?"

I don't understand why you are asking this question as you have already answered it yourself...

The "sender" parameter tells you which item was clicked on.  You are casting it to a Button though when it should be cast to a MenuItem.  If you have more than one type of control being sent to the same handler then you should check the Type first:

    Private Sub ButtonArray_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If TypeOf sender Is Button Then
            Dim btn As Button = CType(sender, Button)
            ' do something with "btn"...
            MsgBox("The Button " & btn.Name & " was clicked.")
        ElseIf TypeOf sender Is MenuItem Then
            Dim mmi As MenuItem = CType(sender, MenuItem)
            ' do something with "mmi"...
            MsgBox("The MenuItem " & mmi.Name & " was clicked.")
        End If
    End Sub
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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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