Solved

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

Posted on 2007-03-22
1
354 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 107
Replacing HTML tags in Textarea/Textbox 5 48
Visual Studio editor? 1 38
How to remove double quotes from a string in VB.Net 5 38
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

18 Experts available now in Live!

Get 1:1 Help Now