i have a tool strip button for bookmarks like the one in Mozilla fire fox or google chrome, i can add button and i can click it but i cant remove it , because that i added context menu to the tool strip with remove, so i want the code for removing the selected tool strip button .

2 Years
Discussion Span
Last Post by scomx


The following code (courtesy of: this post) will hopefully help.

First, add a class level variable to store the button that was clicked (i.e. the one to be removed from the tool strip.

Dim clickedButton As ToolStripItem = Nothing

Then add the following code to the Opening event of your context menu. This code attempts to identify the button that was clicked when the prior to the context menu being displayed:

Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening

    Dim strip As ToolStrip = TryCast(ContextMenuStrip1.SourceControl, ToolStrip)
    clickedButton = Nothing

    If strip IsNot Nothing Then

        Dim position As Point = strip.PointToClient(MousePosition)

        For Each item As ToolStripItem In strip.Items
            If item.Bounds.Contains(position) Then clickedButton = item

    End If

End Sub

Finally, in your remove menu item, add the following code to remove the actual button:

Private Sub RemoveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem.Click

    If clickedButton IsNot Nothing Then ToolStrip1.Items.Remove(clickedButton)

End Sub



thank you very much you don't know how much you helped me

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.