none
Toolstripmenu item and sub item RRS feed

  • Question

  • Hi all

    I have a simple question

    I have a toolstripmenuItem with several items.

    One of them( Item3)  has sub items.

    When I click on a SubItem of Item3 (without clicking on Item3)  I need to know the associate Item (i.e. Item3)

    How to get it ?


    Thursday, October 25, 2018 5:45 PM

Answers

  •  If you have a reference to the 'SubItem' that is clicked,  then check the SubItem.OwnerItem.  You can get the Name, Text,  or whatever property you need to identify the clicked subitem's owner.

        Private Sub SubItemOfItem3ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SubItemOfItem3ToolStripMenuItem.Click
            Dim YourSubItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
            MessageBox.Show(YourSubItem.OwnerItem.Text)
        End Sub
    
     

     The above code does this....


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

    • Proposed as answer by Alex Li-MSFT Friday, October 26, 2018 1:38 AM
    • Marked as answer by Claudio111 Friday, October 26, 2018 7:52 AM
    Thursday, October 25, 2018 8:19 PM

All replies

  •  If you have a reference to the 'SubItem' that is clicked,  then check the SubItem.OwnerItem.  You can get the Name, Text,  or whatever property you need to identify the clicked subitem's owner.

        Private Sub SubItemOfItem3ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SubItemOfItem3ToolStripMenuItem.Click
            Dim YourSubItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
            MessageBox.Show(YourSubItem.OwnerItem.Text)
        End Sub
    
     

     The above code does this....


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

    • Proposed as answer by Alex Li-MSFT Friday, October 26, 2018 1:38 AM
    • Marked as answer by Claudio111 Friday, October 26, 2018 7:52 AM
    Thursday, October 25, 2018 8:19 PM
  • Hi,

    I agree with IronRazerz,add subitems.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim MS As MenuStrip = New MenuStrip()
            Dim tsmi As ToolStripMenuItem = New ToolStripMenuItem("TestItem")
    
            Dim tsmi2 As ToolStripMenuItem = New ToolStripMenuItem("TestSubItem1")
            AddHandler tsmi2.Click, AddressOf DemoClick
            tsmi.DropDownItems.Add(tsmi2)
            Dim tsmi3 As ToolStripMenuItem = New ToolStripMenuItem("TestSubItem2")
            AddHandler tsmi3.Click, AddressOf DemoClick
            tsmi.DropDownItems.Add(tsmi3)
            Dim tsmi4 As ToolStripMenuItem = New ToolStripMenuItem("TestSubItem3")
            AddHandler tsmi4.Click, AddressOf DemoClick
            tsmi.DropDownItems.Add(tsmi4)
            MS.Items.Add(tsmi)
            Me.Controls.Add(MS)
        End Sub
        Private Sub DemoClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim but As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
            MessageBox.Show(but.OwnerItem.Text)
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 26, 2018 1:46 AM