Command Bar Types – Part 2

A few weeks back, I had a posting about how to use the new MRU button list command type when calling AddNamedCommand2. This time, let’s discuss the drop down combo type of command.

VS has a drop-down on one of the command bars that allows you to select the current solution configuration. Because you cannot add new solution configurations by simply typing in the name, this drop-down is read only. To create your own read-only drop-down like this one, you will need to handle two separate commands created with one call to AddNamedCommand2. A call to AddNamedCommand2 appears like so:

Command command = commands.AddNamedCommand2(_addInInstance, “CommandTypesDropDownCombo”, “CommandTypesDropDownCombo”, “Executes the command for CommandTypes”, false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeDropDownCombo);

The important part of this call is the last parameter, vsCommandControlTypeDropDownCombo. Passing this value will cause a command with a UI element of type drop-down combo-box to be created. Now you need to handle the command’s QueryStatus and Exec. Calling AddNamedCommand2 will create two commands, one with the name you specified, and another with the same name but _1 appended to the end. Suppose your Add-in project was named MyAddin, and the class implementing the Add-in was named Connect, therefore, using the name passed from the code above, the command names created would be MyAddin.Connect.CommandTypesDropDownCombo and MyAddin.Connect.CommandTypesDropDownCombo_1.

For the QueryStatus call, you will return supported and enabled for both of these commands. For the Exec method, you will get called with the command name set to MyAddin.Connect.CommandTypesDropDownCombo, but how you react to this command depends on the varIn and varOut parameters. If varIn is not equal to null/Nothing, then a selection was made by the user in the combo-box. If varOut is not equal to null/Nothing, then you are being asked for the text to display in the combo-box as the currently selected item.

The Exec method is also used to retrieve the text of the items to display in the combo box, but the name of the command will be MyAddin.Connect.CommandTypesDropDownCombo_1. When this command is executed, you will pass back an array through the varOut parameter containing strings to display.

So the code for the Exec method looks like this:

if (commandName == “MyAddin.Connect.CommandTypesDropDownCombo”)
    if ((varIn != null) && (varIn is string))
        //We are being told that the user made a selection in the combo, and we need to react to it.
        string selectedText = varIn as string;
        MessageBox.Show(“DropDownCombo: ” + selectedText);
        //The text of the selected item is being asked for, return this through the varOut param.
        varOut = “Item1”;
    handled = true;
if (commandName == “MyAddin.Connect.CommandTypesDropDownCombo_1”)
    //We are being asked for the items in the combo box, return them through the varOut param.
    varOut = new string[] { “Item 1”, “Item 2”, “Item 3”, “Item 4”};
    handled = true;

Comments (4)

  1. Carlos Quintero says:

    So, if I understand well, VS 2005 offers only 2 new command buttons: the MRU button list (like the Recent Files or recent Projects menus) and the dropdown combo (like the Configuration combobox).

    It seems that none of them offers a way to create a dropdown button with a picture, like the "Add New Item" button on the "Standard" toolbar. (How do you call that button? SplitDropDown?)

    What has happened with this promising list that you posted at ?:

    enum vsCommandControlType


    vsCommandControlTypeSeparator = 1,

    vsCommandControlTypeButton = 2,

    vsCommandControlTypeMenuButton = 4,

    vsCommandControlTypeSwatch = 8,

    vsCommandControlTypeSplitDropDown = 16,

    vsCommandControlTypeDropDownCombo = 32,

    vsCommandControlTypeMRUCombo = 64,

    vsCommandControlTypeDynamicCombo = 128

    } vsCommandControlType;

    Yes, I read that the list was subject to change, either with the addition or subtraction of additional elements. Too bad that it was for substraction…


    Carlos Quintero

  2. Today I received in the e-mail a question about how to create a commandbar popup with an image in a Visual

  3. Arujit says:

    Can same be achieved in VS2010 shell

  4. clever101 says:

          hello! how  to Add  combo box to Toolbar add items? i donot know use which interface:

    Command::AddControl or CommandBarControls::Add