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);
    }
    else
    {
        //The text of the selected item is being asked for, return this through the varOut param.
        varOut = "Item1";
    }
    handled = true;
    return;
}
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;
    return;
}