Search code examples
c#winformsvisual-studioenumsvisual-studio-designer

'enum' type breaks in Windows Forms C# designer file


I'm wondering if anyone has experienced this issue before. I'm using C#, .NET 4.5 and Visual Studio 2013.

I have a custom text box, one that basically just inherits from a standard Windows Forms TextBox. On it, there is a property like the following:

public EnumName Property { get; set; }

The EnumName enumeration is defined like this:

[Flags]  
public enum EnumName  
{  
     Value1 = 1,  
     Value2 = 2,  
     Value3 = 3  
}  

When I use my custom control on a form and set the property to Value3, the designer actually does:

control.EnumName = EnumName.Value1 | EnumName.Value2;

This is fine. But lately, when I add a new value to EnumName (e.g., Value4 = 9999), the designer will instead do this:

control.EnumName = EnumName.9999

Does anyone know the reason behind this? It's quite frustrating.


Solution

  • Using the [Flags] attribute, you are saying this enum is a collection of bit flags. This is why 3 is actually 1 | 2.

    01 = 1
    10 = 2
    11 = 3
    

    3 is both one and two.

    You would normally add values for exclusive members of this enum:

    1   0001
    2   0010
    4   0100
    8   1000
    

    A value of 9999 would be the following flags set "10011100001111"

    If there is a reason for the flags attribute, leave it and add values like mentioned above. If not, remove the attribute.