Devonx25
Devonx25

Reputation: 65

How to use dynamically added toolstripmenuitem

I'm trying to make a tool strip item that contains bookmarks and each bookmark should go to the page. How do make each button work?.

    For Each b In New System.IO.DirectoryInfo("Bookmarks").GetFiles
        BookmarksToolStripMenuItem.DropDownItems.Add(b.Name)
    Next

Upvotes: 0

Views: 471

Answers (1)

Epsil0neR
Epsil0neR

Reputation: 1704

You should first create a too ToolStripMenuItem, then add handlers and put it to your toolstripmenu object instead of adding to toolstripmenu a string object.

For Each b In New System.IO.DirectoryInfo("Bookmarks").GetFiles
    Dim menuItem As New ToolStripMenuItem(b.Name)
    'Add any handlers here

    'Click handler to your menuItem.
    AddHandler menuItem.Click, AddressOf menuItem_Click 'CLICK EVENT HANDLER ALSO UNIQUE

    'Add menuItem to ToolStripMenu
    BookmarksToolStripMenuItem.DropDownItems.Add(menuItem)
Next


Private Sub menuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'CODE TO HANDLE CLICK EVENT
End Sub

If you don't know how to dynamicaly add handlers then take a look at examples.

Upvotes: 1

Related Questions