Back to Devexpress

How to: Respond to Selecting ComboBoxEdit's Items

windowsforms-9477-controls-and-libraries-editors-and-simple-controls-examples-how-to-respond-to-selecting-comboboxedits-items.md

latest2.4 KB
Original Source

How to: Respond to Selecting ComboBoxEdit's Items

  • Oct 25, 2019

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.

csharp
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;
     }
 }
vb
ComboBoxEdit1.EditValue = "Regular"
  ComboBoxEdit1.Properties.Items.AddRange(New Object() _
    {"Bold", "Italic", "Regular", "StrikeOut", "Underline"})
'...
  Private Sub ComboBoxEdit1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles ComboBoxEdit1.SelectedIndexChanged
     Dim ec As EnumConverter = TryCast(TypeDescriptor.GetConverter(GetType(FontStyle)), EnumConverter)
     Dim cBox = TryCast(sender, ComboBoxEdit)
     If cBox.SelectedIndex <> -1 Then
         Dim selectedFontStyle As FontStyle = CType(ec.ConvertFrom(cBox.Properties.Items(cBox.SelectedIndex)), FontStyle)
         cBox.Properties.Appearance.FontStyleDelta = selectedFontStyle
     End If
  End Sub