I want to create somewhat of a bookmarks menuitem for a project, but I need to know how to be able to add items to a menustrip item. Like how bookmarks are in firefox or any other program. Can anyone help out?
Printable View
I want to create somewhat of a bookmarks menuitem for a project, but I need to know how to be able to add items to a menustrip item. Like how bookmarks are in firefox or any other program. Can anyone help out?
If you want these menuitems to be saved, then you should look into serializing them into an xml file for loading at runtime.
A MenuStrip is a collection of ToolMenuStripItems. Each ToolMenuStripItem is also a collection of ToolMenuStripItems. With that said, in your code, all you have to do is add an item to whichever ToolMenuStripItem you want.
Let's pretend you have a MenuStrip with a ToolMenuStripItem called tsmBookmarks, and Button1 for adding the items:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.tsmBookmarks.DropDownItems.Add("Your bookmark here") 'or DirectCast(Me.MenuStrip1.Items("tsmBookmarks"), ToolStripMenuItem).DropDownItems.Add("Your bookmark here") End Sub
What if you want to capture when their clicked? Let's add an event handler to those ToolStripMenuItems that get created:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click AddHandler Me.tsmBookmarks.DropDownItems.Add("Your bookmark here").Click, AddressOf tsmBookmarks_ItemClicked End Sub Private Sub tsmBookmarks_ItemClicked(ByVal sender As Object, ByVal e As System.EventArgs) MessageBox.Show("You clicked: " & DirectCast(sender, ToolStripMenuItem).Text) End Sub
Now since you said it was a Bookmark you could attach in the tag of the ToolStripMenuItem the web address or something, like so:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tsmBookmarks_SubItem As New ToolStripMenuItem With {.Text = "VB Forums", .Tag = "http://www.vbforums.com"} Me.tsmBookmarks.DropDownItems.Add(tsmBookmarks_SubItem) AddHandler tsmBookmarks_SubItem.Click, AddressOf tsmBookmarks_ItemClicked End Sub Private Sub tsmBookmarks_ItemClicked(ByVal sender As Object, ByVal e As System.EventArgs) Dim ItemClicked As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) MessageBox.Show("You clicked: " & ItemClicked.Text) MessageBox.Show("Make web browser navigate to tag here: " & ItemClicked.Tag.ToString()) End Sub