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
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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