Results 1 to 4 of 4

Thread: Add ites to munustrip while running?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Add ites to munustrip while running?

    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?

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Add ites to munustrip while running?

    If you want these menuitems to be saved, then you should look into serializing them into an xml file for loading at runtime.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Add ites to munustrip while running?

    Quote Originally Posted by MaximilianMayrhofer View Post
    If you want these menuitems to be saved, then you should look into serializing them into an xml file for loading at runtime.
    Well, I would need to save them too, but what I'm asking is how to actually add the items.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Add ites to munustrip while running?

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Me.tsmBookmarks.DropDownItems.Add("Your bookmark here")
    3.         'or
    4.         DirectCast(Me.MenuStrip1.Items("tsmBookmarks"), ToolStripMenuItem).DropDownItems.Add("Your bookmark here")
    5.     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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         AddHandler Me.tsmBookmarks.DropDownItems.Add("Your bookmark here").Click, AddressOf tsmBookmarks_ItemClicked
    3.     End Sub
    4.  
    5.     Private Sub tsmBookmarks_ItemClicked(ByVal sender As Object, ByVal e As System.EventArgs)
    6.         MessageBox.Show("You clicked: " & DirectCast(sender, ToolStripMenuItem).Text)
    7.     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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim tsmBookmarks_SubItem As New ToolStripMenuItem With {.Text = "VB Forums", .Tag = "http://www.vbforums.com"}
    3.         Me.tsmBookmarks.DropDownItems.Add(tsmBookmarks_SubItem)
    4.  
    5.         AddHandler tsmBookmarks_SubItem.Click, AddressOf tsmBookmarks_ItemClicked
    6.     End Sub
    7.     Private Sub tsmBookmarks_ItemClicked(ByVal sender As Object, ByVal e As System.EventArgs)
    8.         Dim ItemClicked As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
    9.  
    10.         MessageBox.Show("You clicked: " & ItemClicked.Text)
    11.         MessageBox.Show("Make web browser navigate to tag here: " & ItemClicked.Tag.ToString())
    12.  
    13.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width