How to change the Caption of the Command on the toolbar from a Macro in VS2010?

核能气质少年 提交于 2019-12-22 00:20:07

问题


From a macro I am accessing a command that is on the toolbar:

Dim name As String = "Macros.MyMacros.MyMacros.ToggleExceptions"
Dim cmd As EnvDTE.Command = DTE.Commands.Item(name)

How do I now change the caption of the command on the toolbar? It does not seem to have the necessary properties. Do I need to cast it to something else?


回答1:


I've implemented it:

Private Sub Main()
    Const BAR_NAME As String = "MenuBar"
    Const CTL_NAME = "Foo"

    ChangeCommandCaption(BAR_NAME, CTL_NAME, "Bar")
End Sub

Private Sub ChangeCommandCaption(ByVal cmdBarName As String, ByVal ctlName As String, ByVal caption As String)
    Dim bars As Microsoft.VisualStudio.CommandBars.CommandBars

    bars = DirectCast(DTE.CommandBars, Microsoft.VisualStudio.CommandBars.CommandBars)
    If bars Is DBNull.Value Then Exit Sub

    Dim menuBar As CommandBar = bars.Item(cmdBarName)
    If menuBar Is DBNull.Value Then Exit Sub

    Dim cmdBarCtl As CommandBarControl

    Try
        cmdBarCtl = menuBar.Controls.Item(ctlName)
        If cmdBarCtl Is DBNull.Value Then Exit Sub
    Catch ex As Exception
        Exit Sub
    End Try

    cmdBarCtl.Caption = caption
End Sub


来源:https://stackoverflow.com/questions/7923244/how-to-change-the-caption-of-the-command-on-the-toolbar-from-a-macro-in-vs2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!