Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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 .
Posted
Comments
Sergey Alexandrovich Kryukov 6-May-15 15:32pm    
It's parent should have Remove method. You need to point to exact UI types you are using.
—SA
Member 11667739 7-May-15 1:39am    
visual studio 2013
Ralf Meier 7-May-15 6:51am    
Yoiu don't answered the question of Sergey ...
Could you show us your current code (or the significant part of it) ?
Member 11667739 7-May-15 7:54am    
this is the the code for adding toolstripbutton with url,icon and text to the database and it working perfect


Private Sub bm_Click(sender As Object, e As EventArgs) Handles bm.Click
addb()
addbtd()
end sub

Private Sub addbtd()
Dim browser As wbf = Me.TabControl1.SelectedTab.Tag
conn()
Try
sql = "select * from bookmark where name like '" & TabControl1.SelectedTab.Text & "' "
cm = New OleDbCommand(sql, con)
rdr = cm.ExecuteReader
If rdr.Read Then
Try
sql = "update bookmark set ico = @picture , url = '" & browser.Url.ToString & "' where name ='" & TabControl1.SelectedTab.Text & "'"
cm = New OleDbCommand(sql, con)
pbp()
cm.ExecuteNonQuery()
Catch ex As Exception
'MsgBox(ex.Message)
End Try
Else
Try
sql = "insert into bookmark values(@picture,'" & TabControl1.SelectedTab.Text & "','" & browser.Url.ToString & "')"
conn()
cm = New OleDbCommand(sql, con)
pbp()
cm.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Catch ex As Exception
' MsgBox(ex.Message)
End Try

End Sub

Private Sub addb()
Dim browser As wbf = Me.TabControl1.SelectedTab.Tag
conn()
Try
sql = "select * from bookmark where url like '" & browser.Url.ToString & "' "
cm = New OleDbCommand(sql, con)
rdr = cm.ExecuteReader
If rdr.Read Then
Else
Dim newbookmark As New ToolStripButton
newbookmark.Text = TabControl1.SelectedTab.Text
newbookmark.Image = ficon.Image
ts2.Items.Add(newbookmark)
newbookmark.ForeColor = Color.White
AddHandler newbookmark.Click, AddressOf nbmc
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub



Private Sub pbp()
Dim ms As New MemoryStream()
Try
ficon.Image.Save(ms, Imaging.ImageFormat.Bmp)
Dim data As Byte() = ms.GetBuffer()
Dim p As New OleDbParameter("@picture", SqlDbType.Image)
p.Value = data
cm.Parameters.Add(p)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub



and this is who add handler to the toolstripbutto

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

Dim browser As wbf = TabControl1.SelectedTab.Tag


If TypeOf sender Is ToolStripButton Then
Try
browser.Navigate(CType(sender, ToolStripButton).Tag)
Catch ex As Exception
End Try

End If
End Sub

Private Sub cbc()
For Each item As ToolStripButton In ts2.Items
AddHandler item.Click, AddressOf bck
Next
End Sub

Private Sub bck(ByVal sender As Object, ByVal e As EventArgs)
url.Text = CType(sender, ToolStripButton).Tag
bnav()
End Sub

Private Sub bnav()
Dim browser As wbf = TabControl1.SelectedTab.Tag
browser.Navigate(url.Text)

End Sub



and this code load the toolstripbuttons from the database when the form load

Private Sub swb_Load(sender As Object, e As EventArgs) Handles Me.Load

Try
conn()
sql = "select * from bookmark"
cm = New OleDbCommand(sql, con)
rdr = cm.ExecuteReader
While rdr.Read
'L1.Items.Add(rdr.Item(2))
Dim nbm As New ToolStripButton
nbm.Text = rdr.Item(1)
nbm.ForeColor = Color.White
Dim bt As Byte() = CType(rdr.Item(0),

This might give you an idea as how to remove a specific ToolStripButton:

VB
Dim button As ToolStripButton = Nothing
       For Each c As Object In ToolStrip1.Items
           Dim t As Type = c.GetType
           If t.Name = "ToolStripButton" Then
               button = DirectCast(c, ToolStripButton)
               If button.Tag IsNot Nothing AndAlso
                   button.Tag.ToString = "buttonToBeRemoved" Then
                   Exit For
               End If
               button = Nothing
           End If
       Next
       If button IsNot Nothing Then
           ToolStrip1.Items.Remove(button)
       End If


regs

ron O.
 
Share this answer
 
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
        Next
    End If
End Sub</pre>
Finally, in your remove menu item, add the following code to remove the actual button:

<pre>
Private Sub RemoveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem.Click
    If clickedButton IsNot Nothing Then ToolStrip1.Items.Remove(clickedButton)
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900