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

1

1

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?

Kearney answered 27/10, 2011 at 22:35 Comment(1)
That macro doesn't actually accesses a command on the toolbar. DTE.CommandBars would be a starting point, I think.Nabonidus
K
3

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
Kearney answered 28/10, 2011 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.