none
Adding item to Menustrip Dynamically RRS feed

  • Question

  • Hi all

    I need to add dynamically Sub items to a MenuStrip Item (here CmdFiltra) getting the name from Datagridview Column Headertext and then Handle the click for each Sub Item

    I'm using this code

    Public Sub AddCmdFiltraItems()
            For i = 0 To DGV.Columns.Count - 1
                If i > 1 Then
                    Dim name As String = DGV.Columns(i).HeaderText
                    Dim item As New ToolStripMenuItem(name)
                    CmdFiltra.DropDownItems.Add(item)
                    AddHandler item.Click, AddressOf FiltraItemClicked
                End If
            Next
        End Sub
        Private Sub FiltraItemClicked(ByVal sender As Object, ByVal e As EventArgs)
            MessageBox.Show(sender.ToString)
            MessageBox.Show("Clicked")
        End Sub
    

    I start to add sub Items from column 1 

    Now in the click Handler I need to Know the column Index corrisponding to the name of sub Item.

    How can I find it  ?

    Sunday, August 26, 2018 5:50 PM

Answers

  •  Better yet,  you could assign a reference of the DataGridViewColumn to the Tag property of the ToolStripMenuItem.  This way,  if the columns are arrange-able and they are arranged by the user,  you will still get the correct index of the column. 

        Public Sub AddCmdFiltraItems()
            For i As Integer = 0 To DGV.Columns.Count - 1
                If i > 1 Then
                    Dim name As String = DGV.Columns(i).HeaderText
                    Dim item As New ToolStripMenuItem(name)
                    item.Tag = DGV.Columns(i) 'assign a reference to the DataGridViewColumn to the Tag property of the ToolStripMenuItem
                    CmdFiltra.DropDownItems.Add(item)
                    AddHandler item.Click, AddressOf FiltraItemClicked
                End If
            Next
        End Sub
    
        Private Sub FiltraItemClicked(ByVal sender As Object, ByVal e As EventArgs)
            Dim tsmi As ToolStripMenuItem = CType(sender, ToolStripMenuItem) 'cast the 'sender' object to a ToolStripMenuItem to get a reference to the ToolStripMenuItem that was clicked
            Dim column As DataGridViewColumn = CType(tsmi.Tag, DataGridViewColumn) 'cast the Tag object back to a DataGridViewColumn
            MessageBox.Show(column.Index.ToString)
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Claudio111 Monday, August 27, 2018 7:23 AM
    Sunday, August 26, 2018 7:37 PM

All replies

  •  Assign the column index to the Tag property of the ToolStripMenuItem.  Then you can cast the 'sender' object in the click event back to a ToolStripMenuItem and retrieve the column index from the Tag property.

        Public Sub AddCmdFiltraItems()
            For i As Integer = 0 To DGV.Columns.Count - 1
                If i > 1 Then
                    Dim name As String = DGV.Columns(i).HeaderText
                    Dim item As New ToolStripMenuItem(name)
                    item.Tag = i 'assign the column index (integer) to the Tag property of the ToolStripMenuItem
                    CmdFiltra.DropDownItems.Add(item)
                    AddHandler item.Click, AddressOf FiltraItemClicked
                End If
            Next
        End Sub
    
        Private Sub FiltraItemClicked(ByVal sender As Object, ByVal e As EventArgs)
            Dim tsmi As ToolStripMenuItem = CType(sender, ToolStripMenuItem) 'cast the 'sender' object to a ToolStripMenuItem to get a reference to the ToolStripMenuItem that was clicked
            Dim columnIndex As Integer = CInt(tsmi.Tag) 'convert the 'Tag' object back to an integer
            MessageBox.Show(columnIndex.ToString)
        End Sub
    


    If you say it can`t be done then i`ll try it

    Sunday, August 26, 2018 7:16 PM
  •  Better yet,  you could assign a reference of the DataGridViewColumn to the Tag property of the ToolStripMenuItem.  This way,  if the columns are arrange-able and they are arranged by the user,  you will still get the correct index of the column. 

        Public Sub AddCmdFiltraItems()
            For i As Integer = 0 To DGV.Columns.Count - 1
                If i > 1 Then
                    Dim name As String = DGV.Columns(i).HeaderText
                    Dim item As New ToolStripMenuItem(name)
                    item.Tag = DGV.Columns(i) 'assign a reference to the DataGridViewColumn to the Tag property of the ToolStripMenuItem
                    CmdFiltra.DropDownItems.Add(item)
                    AddHandler item.Click, AddressOf FiltraItemClicked
                End If
            Next
        End Sub
    
        Private Sub FiltraItemClicked(ByVal sender As Object, ByVal e As EventArgs)
            Dim tsmi As ToolStripMenuItem = CType(sender, ToolStripMenuItem) 'cast the 'sender' object to a ToolStripMenuItem to get a reference to the ToolStripMenuItem that was clicked
            Dim column As DataGridViewColumn = CType(tsmi.Tag, DataGridViewColumn) 'cast the Tag object back to a DataGridViewColumn
            MessageBox.Show(column.Index.ToString)
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Claudio111 Monday, August 27, 2018 7:23 AM
    Sunday, August 26, 2018 7:37 PM