Skip to main content

How to: Respond to Selecting ComboBoxEdit's Items

Consider a combo box editor displaying different font styles (regular, bold, italic, underline, strikeout). When you select an item from the dropdown list, a corresponding font style is applied to the combo box editor.

Combo box items in this example represent strings identifying names of specific font styles. In the ComboBoxEdit.SelectedIndexChanged event handler, we determine the currently selected item and convert it to a corresponding value of type FontStyle. Then the new font is applied to the editor.

The following image shows a combo box editor when the Underline item is selected.

ComboBox_SelectedindexChanged_example

  comboBoxEdit1.EditValue = "Regular";
  comboBoxEdit1.Properties.Items.AddRange(new object[] 
    {"Bold", "Italic", "Regular", "StrikeOut", "Underline"});
  comboBoxEdit1.SelectedIndexChanged += new System.EventHandler(
    this.comboBoxEdit1_SelectedIndexChanged);
//...
 private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e) {
     EnumConverter ec = TypeDescriptor.GetConverter(typeof(FontStyle)) as EnumConverter;
     var cBox = sender as ComboBoxEdit;
     if (cBox.SelectedIndex != -1) {
         FontStyle selectedFontStyle = (FontStyle)ec.ConvertFrom(
           cBox.Properties.Items[cBox.SelectedIndex]);
         cBox.Properties.Appearance.FontStyleDelta = selectedFontStyle;
     }
 }