Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Universal Enum Editor for WinForms

0.00/5 (No votes)
26 Feb 2016 1  
An Editor Control to edit both Flags and non-Flags Enums

Introduction

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.

Background

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
{
    None,
    Cars,
    MotorCycles,
    Trucks,
    Buses
}

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>
[Flags]
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.

History

  • Rev 1: The ItemHeight property did not work correctly

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here