Select One Enum Value Easily [EnumSelector Winforms Control]

If you create a WinForms application and wish to allow your users to select one enum value from all enum values, you can use EnumSelector. The EnumSelector, a custom WinForms control, simplifies the creation of user interfaces based on enum types. It makes it easy to choose one value from all possible values of the enum type.

How To Select One Enum Value With EnumSelector?

EnumSelector control allows the user to select one enum value from all possible values of a specific enum type. To use the control, You should inherit from EnumSelector with enum type.

The Selected property holds the currently selected value. This property is always synchronized with the user interface. In other words, When the user selects a new enum value, the property value is updated to a new selection immediately. When the property is updated in the program, the user interface’s selected value is selected and displayed.

When the user selects a new enum value, the SelectionChanged event is raised. You can use the Selected property of the EnumSelector object, which raised the event, to find out the newly selected value.

By default, the names of the enum values will be displayed in the user interface. While this is very convenient, sometimes this is not suitable (For example, a more detailed name should be displayed, the jargon used to name the values is unfamiliar to the target audience, or the values need to be translated to another language). In this case, You can set the AllowFormat property and override the OnFormat method. This method accepts the enum value to be displayed and should return a string representing the value displayed on the user interface.

WinForms Demo – Select One Enum Value

The demo program displays an image. It allows the user to select which image will be displayed and how it should be displayed. To achieve this, we create two EnumSelector sub-classes- DisplayStyleSelector and DisplayImageSelector.

Select One Enum Value with EnumSeletor
Select One Enum Value with EnumSeletor

DisplayStyleSelector – EnumSelector Subclass

The user can also select how the image should be displayed.

To implement this feature, we create DisplayStyleSelector to allow the user to select how the image should be displayed. The user can select one of the ImageLayout enum values: None,Tile,Zoom,Center and Stretch.

DisplayStyleSelector To Select One Enum Value Of ImageLayout
 public partial class DisplayStyleSelector : EnumSelector<ImageLayout> {
 }

When the user selects a new ImageLayout value, we update the BackgroundImageLayout of the Picture Box.

The SelectionChanged handler of DisplayStyleSelector
 private void winDisplayStyle_SelectionChanged(object sender,EventArgs e) {
   winPictureBox.BackgroundImageLayout = winDisplayStyle.Selected;
 }

The DisplayImageSelector – EnumSelector Subclass

The user can also select one of the images to be displayed: rectangle in red, rectangle in yellow and rectangle in green.

To implement this feature, we create a new enum DisplayImage and a new DisplayImageSelector. Please note that we change the displayed text in the user interface by setting the AllowFormat property in the constructor and overriding OnFormat which returns the name of the value with the prefix “Rectangle in “.

DisplayImageSelector for selecting DisplayImage enum values
 public enum DisplayImage : int {
   Green,
   Yellow,
   Red,
 }

 public partial class DisplayImageSelector : EnumSelector<DisplayImage> {
   public DisplayImageSelector() {
     AllowFormat = true;
   }
   public override string OnFormat(DisplayImage value) {
     return "Rectangle in " + value.ToString();
   }
 }

When the user selects a new DisplayImage value, we update the BackgroundImage of the Picture Box based on the new selection.

The SelectionChanged handler of DisplayImageSelector
 private void winDisplayImage_SelectionChanged(object sender,EventArgs e) {
     switch ( winDisplayImage.Selected) {
         case DisplayImage.Green:
           winPictureBox.BackgroundImage = global::Napuzba.Demo.Properties.Resources.green;
           break;
         case DisplayImage.Yellow:
           winPictureBox.BackgroundImage = global::Napuzba.Demo.Properties.Resources.yellow;
           break;
         case DisplayImage.Red:
           winPictureBox.BackgroundImage = global::Napuzba.Demo.Properties.Resources.red;
           break;
     }
 }

The Code Behind EnumSelector

Creating the EnumSelector Winforms Control

To create the control, choose in the project context menu Add > New Item ... > Visual C# Items > Windows Forms > UserControl.

EnumSelector - Create Control
EnumSelector – Create Control

Type the name “EnumSelector.cs” and click Add. A new design screen of the new user control appears. Drag a ComboBox control into the design view and update its properties (Name) to winDropDown, DropDownStyle to DropDownList and Dock to Fill. Size the control to the desired height so the ComboBox will fill the entire space.

Adding enum values

As we want this control to display enum values we make it generic with the enum type TT EnumSelector

EnumSelector Inherits From UserControl
 public partial class EnumSelector<TT> : UserControl {

In the constructor we fill winDropDown with the enum values with help of Enum.GetValues EnumSelector constructor

EnumSelector – Add Enum Values
 public EnumSelector() {
     InitializeComponent();
     foreach (Object oo in Enum.GetValues(typeof(TT))) {
         winDropdown.Items.Add((TT)oo);
     }
 }

The Selected Property Of EnumSelector

The Selected property should be synchronized with the user interface – In Selected property setter we update winDropdown.SelectedItem to the value and the property getter will return the _selected field.

The selected property of EnumSelector
 public TT Selected {
   get {
     return _selected;
   }
   set {
     winDropdown.SelectedItem = value;
   }
 }
 private TT _selected;

The SelectionChanged Event Of EnumSelector

To ensure that Selected property getter will always return the selected value in the user interface, we will add a new handler winDropdown_SelectionChanged to winDropDown’s SelectionChanged event using the properties window. We will also raise our SelectionChanged event.

EnumSelector - Add SelectionChanged handler
EnumSelector – Add SelectionChanged handlerde

In this handler we will update the field and raise our SelectionChanged event.

The SelectionChanged event of EnumSelector
 public event EventHandler SelectionChanged;
 private void winDropdown_SelectionChanged(object sender,EventArgs ee) {
   _selected = (TT)winDropdown.SelectedItem;
   if (SelectionChanged != null) {
     SelectionChanged(sender,ee);
   }
 }

Use OnFormat To Change The Display Strings

In order to update names of the values, we add the OnFormat virtual method. We also add new handler winDropdown_Format to winDropDown’s Format event which allows to us to convert a value to its associated display string.

The OnFormat method of EnumSelector
 public virtual string OnFormat(TT value) {
   return value.ToString();
 }
 private void winDropdown_Format(object sender,ListControlConvertEventArgs ee) {
   ee.Value = OnFormat((TT)ee.ListItem);
 }

The AllowFormat property connect the handler to the event as requested

The AllowFormat property of EnumSelector
 public bool AllowFormat {
   get {
     return _allowFormat;
   }
   set {
     if (value == _allowFormat) {
       return;
     }
     _allowFormat = value;
     if (value) {
       winDropdown.Format += winDropdown_Format;
     } else {
       winDropdown.Format -= winDropdown_Format;
     }
   }
 }
 private bool _allowFormat = false;

EnumSelector Summary

We see how can we create EnumSelector – a WinForms custom control to select one enum value. We also create a demo application. With the help of EnumSelector and minimal lines of code, we could create two specific controls that allows to select one value of enum type.

If you create a WinForms application and wish to allow your users to select many enum value from all enum values, check Select Many Enum Values Easily.

Leave a Reply

Your email address will not be published. Required fields are marked *