Whats This Command Bar ?

One of the challenges I faced while creating my TSQL Smells visual studio (SSDT) add in, was trying to find out exactly what the names of the Command Bar objects internal to Visual Studio were.

Most of the advice on the internet is fairly haphazard ie guess/trial and error, but there is a simpler way.

Here http://1drv.ms/1fsR3n3 is a C# project that will add the Command Bar name into every command bar.  After dropping that into you addins directory you should now see something like this:

cmdbar

The code itself is pretty simple :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    if(connectMode == ext_ConnectMode.ext_cm_UISetup)
    {
        object []contextGUIDS = new object[] { };
        string toolsMenuName = "Tools";
 
        Commands2 commands = (Commands2)_applicationObject.Commands;
        Microsoft.VisualStudio.CommandBars.CommandBars Bars = (Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars;
 
       for(int x=1;x<Bars.Count;x++){
           Microsoft.VisualStudio.CommandBars.CommandBar Cb = Bars[x];
           try
           {
                Command command = commands.AddNamedCommand2(_addInInstance,"Addin"+x.ToString(),
                    "This CmdBar is ["+Cb.Name+"]" , "Executes the command for My Addin",
                    true, 59, ref contextGUIDS,
                    (int)vsCommandStatus.vsCommandStatusSupported +
                    (int)vsCommandStatus.vsCommandStatusEnabled,
                    (int)vsCommandStyle.vsCommandStylePictAndText,
                    vsCommandControlType.vsCommandControlTypeButton);
 
                if ((command != null))
                {
                     CommandBarControl ctrl =
                    (CommandBarControl)command.AddControl(Cb, 1);
 
                    ctrl.TooltipText = "Executes the command for MyAddin";
                }
            }
            catch
            {
            }
        }
    }
}
 
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
    if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
    {
        if(commandName.StartsWith("WhatsThisCommandBar.Connect.Addin"))
        {
             status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
             return;
        }
    }
}

Hope this helps someone struggling to find the correct command bar name.

Leave a Reply

Your email address will not be published. Required fields are marked *