Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

632 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