Solved

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

Posted on 2007-03-22
1
350 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
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
"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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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