如何使用代码删除工具条按钮 [英] how to remove tool strip button using code

查看:85
本文介绍了如何使用代码删除工具条按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具条按钮,用于书签,如Mozilla火狐或谷歌浏览器,我可以添加按钮,我可以点击它,但我不能删除它,因为我添加了上下文菜单到工具条删除,所以我想要删除所选工具条按钮的代码。

解决方案

这可能会让你知道如何删除特定的ToolStripButton:



  Dim 按钮 As  ToolStripButton = 没有 
对于 每个 c 作为 对象 ToolStrip1.Items
Dim t As Type = c。 GetType
如果 t.Name = ToolStripButton 然后
button = DirectCast (c,ToolStripButton)
如果按钮.Tag IsNot 没什么 AndAlso
button.Tag.ToString = buttonToBeRemoved 然后
退出 对于
结束 如果
button = Nothing
结束 如果
下一步
如果按钮 IsNot Nothing 然后
ToolStrip1.Items.Remove(按钮)
结束 如果





regs



ron O。


首先,添加一个类级别变量来存储被点击的按钮(即要从工具条中移除的那个。



 Dim clickedButton As ToolStripItem = Nothing 



然后将以下代码添加到上下文菜单的Opening事件中。此代码尝试识别在显示上下文菜单之前单击的按钮:



 Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs)处理ContextMenuStrip1.Opening 
Dim strip As ToolStrip = TryCast(ContextMenuStrip1.SourceControl,ToolStrip)
clickedButton = Nothing
如果strip IsNot Nothing则
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如果
End Sub< / pre>
最后,在您的删除菜单项中,添加以下代码以删除实际按钮:

< 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


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 .

解决方案

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

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.


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


这篇关于如何使用代码删除工具条按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆