Click here to Skip to main content
14,172,047 members
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 25 Feb 2016
Licenced CPOL

A Universal Enum Editor for WinForms

, 26 Feb 2016
Rate this:
Please Sign up or sign in to vote.
An Editor Control to edit both Flags and non-Flags Enums


Enums are very useful and provide a convenient method for defining standard lists or options such as the DayOfWeek or the FormBorderStyle property in Windows Forms.


I use Enums wherever possible in my code and usually provide GUI editing functions using a ComboBox or CheckedListBox. This is OK for most simple Enum types, however Enums can actually be quite complex as explained below and something more than a basic CheckListBox may be required for editing.

Many simple Enums are defined using just a list of element names as shown here. If required, you can optionally specify the underlying data type used to store the actual value of the Enum but this is not required and will default to int.

/// <summary>
/// The Simple Enum
/// </summary>
public enum Vehicles

If you don't specify the numeric value of each element, the compiler will automatically supply these values starting at 0 for the first item and incrementing by one for each subsequent item. If required however, you can specify the actual numeric value of each item as shown below:

/// <summary>
/// The Simple Enum
/// </summary>
public enum Vehicles : int
    None = 0,
    Cars = 1,
    MotorCycles = 2,
    Trucks = 3,
    Buses = 4

Here lies the first interesting point... you can specify multiple elements with the same numeric value as shown below. Notice that SaloonCars and SportsCars have the same numeric value of 1.

/// <summary>
/// Enum with more than 1 element having the same numeric value.
/// </summary>
public enum Vehicles : int
    None = 0,
    SaloonCars = 1,
    SportsCars = 1,
    MotorCycles = 2,
    Trucks = 3,
    Buses = 4

Flags Enums

With simple Enum types, you can only select one value from the list of values. Flags type Enums are decorated with the FlagsAttribute ([Flags]). With Flags Enums, each element within the Enum represents one or more bits set within the underlying value. As with simple Enums, more than one element can have the same numeric value. Elements within Flags Enums can also represent a set of other values within the Enum as shown below:

/// <summary>
/// A typical Flags Enum
/// </summary>
public enum Targets: int
    Users = 1,
    Computers = 2,
    UserAndComputer= 3,
    Servers = 4,
    All = 7

Notice that element 'All' (7) represents a binary value of 111 which effectively will match Users, Computers and Servers.

If you set the value of an Enum to a value that does not match one or more of the defined elements, you will not get an Exception and when calling the ToString() method, it will display the string representation of the numeric value rather than the list of Enum names. Developers often have to include code to detect this situation and throw ArgumentOutOfRange Exceptions for invalid values. Also note that not all Enums have a value representing numeric 0 so even the default value of an Enum may not match any of the defined flag names.

var targets = Targets.Users | Targets.Servers; 
Console.WriteLine(targets); // Will display "Users, Servers"

var targets = Targets.Users | Targets.Computer;
Console.WriteLine(targets); // Will display "UsersAndComputers"

// Set the enum to an invalid (unsupported) value
var targets = (Targets)8;
Console.WriteLine(targets); // Will display "8" as 8 is not a supported value

// Set the enum to the default value
targets = (Targets)0;
Console.WriteLine(targets); // Will display "0" as 0 is not a supported value

The Enum Editor

I have developed a Universal Enum editor control based on the CheckistBox control. It will work correctly with Enums for all of the supported underlying data types. It handles both simple and flags Enums. If required, simple Enum types will be rendered as RadioButtons rather than CheckBoxes.

The Editor also handles Enums which have multiple elements with the same underlying values. An example of this is the System.Security.AccessControl.FileSystemRights Enum. This is a complex Enum used to specify the file system access rights and relates very closely to the Window file system security dialogs.

Using the Code

To use the code, simply unzip the demo project and run it. The editor control is the 'EnumEditor' custom control derived from the CheckListBox control.

Points of Interest

I originally thought that Enums were quite simple but the more I investigated, I realized that there are a lot more subleties than I first thought and so I tried quite a few versions of the Enum Editor before I was finally happy.


  • Rev 1: The ItemHeight property did not work correctly


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alan Seedhouse
Founder Own
United Kingdom United Kingdom
I have been involved with IT for far too many years than I can remember...right from the beginning of the PC industry in the UK! I have worked in both hardware and software environments and for the past 7 years I have been self employed developing software to manage networks within educational establishments. I work mainly with .Net and WinForms as most of the apps I work with have been developed over a number of years and have not yet been migrated to WPF or Modern Apps. I have a great deal of experience working with Active Directory and integrating applications with existing Microsoft technologies such as Exchange, Microsoft Deployment Tools, Office 365, etc.

I still get a huge amount of satisfaction from developing software and hope to continue until I retire!

You may also be interested in...

Comments and Discussions

SuggestionWrong type Pin
Afzaal Ahmad Zeeshan26-Feb-16 7:23
mveAfzaal Ahmad Zeeshan26-Feb-16 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190524.2 | Last Updated 26 Feb 2016
Article Copyright 2016 by Alan Seedhouse
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid