While trying to write a custom control, I ran into a problem with the System.Windows.Forms.TextFormatFlags enumeration in combination with the Visual Studio editor (2005/2008). The reason for this problem is apparently due to the fact that this enumeration has several members that display a null value. Selecting any of these elements (GlyphOverhangPadding, Left, Default, Top) causes the editor to set the property
this.customControl.TextFormatFlags = System.Windows.Forms.TextFormatFlags.GlyphOverhangPadding;
The code compiles as expected. However, the selection of any nonzero element (for example, "on the right") from the editor properties grid leads to the following:
this.customControl.TextFormatFlags = System.Windows.Forms.TextFormatFlags.Left, Default, Top, Right;
Obviously, this does not compile. Selecting more than one non-zero element (via UITypeEditor, for example, "Right | Bottom") leads to the following:
this.customControl.TextFormatFlags = ((System.Windows.Forms.TextFormatFlags)((System.Windows.Forms.TextFormatFlags.Left, Default, Top, Right | System.Windows.Forms.TextFormatFlags.Left, Default, Top, Bottom)));
As you can see, the editor adds three of the four members with a zero value to any selected item.
If you want to reproduce this problem:
- Create a new project in Visual Studio 2005/2008 (Windows Forms Application)
- Add a custom control to the project.
Add private field and public property to the new class:
private TextFormatFlags tff = TextFormatFlags.Default;
public TextFormatFlags TFFProperty {get {return this.tff; } set {this.tff = value; }}
Compile code
- Open Form1 in the constructor and add CustomControl1 to it
- The code compiles fine
- Now open the properties of CustomControl1 in the PropertyGrid editor
- You should see TFFProperty in the Miscellaneous section.
- The property offers several values, most of which contain a comma.
- Choosing any of the values ββwith a comma (for example, "Left", "Default", "Top", "Horizontal center") leads to the absence of compilation code
The same thing happens if you create your own enumeration with the Flags attribute and add more than one member matched to zero (which is a kind of malformed enum flags?). I checked that this is not an error with the UITypeEditor that I am using (the same problem occurs without using UITypeEditor). I tried to get around the converter problem so far without success. If anyone has any ideas on how to solve this problem, I would be happy to hear them.
Markus pfeiffer
source share