October 21, 2009

ComboBox Control A ribbon ComboBox control is basically the normal ComboBox control that we all love, but with the additional feature of dividing the items into categories. A category is not an item and cannot be selected from the ComboBox. It is only used to organized the items.

ComboBox Properties Every ribbon control has properties that defines the way it looks and behaves. Here is a quick review of ComboBox properties, divided into logical groups:

ComboBox Value Related Properties

Items Source – The list of ComboBox items. It is exposed as an IUICollection where every element in the collection is of type: IUISimplePropertySet. More on this later. Property Identifier: UI_PKEY_ItemsSource

Categories – The list of categories. Also exposed as an IUICollection of IUISimplePropertySet elements. Property Identifier: UI_PKEY_Categories

Selected Item – The index of the selected item in the ComboBox. If nothing is selected returns UI_Collection_InvalidIndex, which is a fancy way to say -1. Property Identifier: UI_PKEY_SelectedItem

String Value – The current string in the ComboBox. This can be a string that isn’t one of the possible items in the ComboBox, in case the ComboBox has IsEditable set to true. Property Identifier: UI_PKEY_StringValue

ComboBox Appearance Related Properties

Representative String – A string that represents the common value for the ComboBox. This is used to calculate the width of the ComboBox, so you should set here the longest string you forecast. Note that it doesn’t have to be an actual value, it can be also: “XXXXXXXX”. Property Identifier: UI_PKEY_RepresentativeString

IsAutoCompleteEnabled – Flag that indicated whether to complete the words as you write.

IsEditable – Flag that indicates whether to allow free writing in the ComboBox.

ResizeType – Allow resize of the ComboBox. Can be NoResize or VerticalResize.

Using ComboBox – Code Behind In a similar way to the spinner control, I’ve created a helper classes that encapsulates the interaction between the ComboBox and ribbon framework. To use the ComboBox, create a RibbonComboBox instance, passing to the constructor the Ribbon instance and command ID of the ComboBox:

Note: We set the RepresentativeString property BEFORE the initializing the ribbon framework. This is because for some reason the framework reads this property only once, when the ribbon is initialized. This means that if you change it after initialization it will have no affect since the framework doesn’t reads this property anymore. By the way, according to the current documentation of the ribbon framework, this property is not part of the ComboBox, but as mentioned earlier, this property controls the width of the ComboBox.

In the next code snippet you can see how to use another helper class, named GalleryItemPropertySet. This class represents a container for properties of a single element in an IUICollection.

Update (18.11.2009): The updated version of the Ribbon class provides an implementation for IUICommandHandler, so the user doesn’t need to implement Execute and UpdateProperty methods anymore.

IUICollection Events Objects that implements IUICollection interface usually expose an OnChanged event that is called when the collection has changed due to: Insert item, Remove item, Replace item, Reset collection.

6 comments

I am trying to populate 2 combo boxes with string items. The first combobox is populated without problems but when i try to populate the second combobox, i get the following error “The type of this variable is not support (‘0’)”.

I think that the combobox issue was already solved a few weeks ago.
Can you download the latest changeset under the SOURCE section
Note that the latest changeset has some minor bug fixes that doesn’t appear in the latest RELEASE download.

Please let me know your results.
If the problem still continues, it would be helpful if you send me a small project demonstrating the problem.