Creating contextmenustrip using all the dropdownitems from a toolstripmenuitem including toolstripseperator

ロック済み Creating contextmenustrip using all the dropdownitems from a toolstripmenuitem including toolstripseperator

  • 2012年4月28日 7:19
     
     
    I have a Menustrip which displays a large number of menus and submenus. Like File, Edit, Master, transcation etc.. I want display a context menu dynamically generated from the submenus of menus strip please help...

全部回复

  • 2012年4月30日 9:27
    版主
     
     

    Hi Shri,

    Welcome to the MSDN forum.

    To generate a menu strip dynamically, you can use the following codes:

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

            Dim ms As New MenuStrip()

            Dim testItem As New ToolStripMenuItem("Test", Nothing, New EventHandler(AddressOf item_Click))

            ms.Items.Add(testItem)

            Me.Controls.Add(ms)

            ms.Visible = True

        End Sub

        Private Sub item_Click(ByVal sender As Object, ByVal e As EventArgs)

            Stop

        End Sub

    This reference link may be helpful also: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/2160832d-ebd3-476a-892d-2a1e1403b532

    If I misunderstood anything, please feel free and let me know.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

  • 2012年5月9日 9:53
     
     

     Thanks Shanks Zen,

    But I am Trying to generate Context menu using Menu Strip which is already created at design time. I want both the Menu Item and  Context menu Item should share a same event ... 

  • 2012年5月10日 9:56
     
      包含代码

    Hello

    A quick test on this, and can't a solution for binding event for them :(

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Menu As New ContextMenuStrip()
            For Each t As ToolStripMenuItem In MenuStrip1.Items
                Dim test As New ToolStripMenuItem(t.Text)
                For Each s As ToolStripDropDownItem In t.DropDownItems
                    test.DropDownItems.Add(s.Text)
                Next
                Menu.Items.AddRange(New ToolStripItem() {test})
            Next
        End Sub
    
    Whatever, hope some members will jump into this case because I'm expecting a solution for this too.

  • 2012年6月23日 4:53
     
     

    I have found the unique solution. 

    I just remove the dropdownitems of all the menustrip item and made them as a context menu.

     the context menu can be added to the MenustripItems dropdown Item. Something like

    MenuStripItem.DropDown =ContextMenu1

    this can be used for both Mwnu dropdown or Context menu having a same event.


    Shrinidhi Acharya

    • 已建议为答案 Gagi78 2012年10月8日 20:04
    • 取消建议作为答案 Gagi78 2012年10月8日 20:04
    •  
  • 2012年10月8日 20:07
     
     
    You can add lots of menu and context menu just make combination between them, and make proper combination for your solution.

    Gagi78