For an overview of properties see Properties. There are a few important considerations to remember when defining a property:
set
method after setting the property. Invalidate in turn calls the OnPaint method that redraws the control. Multiple calls to Invalidate result in a single call to OnPaint for efficiency.Note If a type converter or a UI type editor is not available for your custom property, you can implement one as described in Enhancing Design-Time Support.
The following code fragment defines a custom property EndColor
for the custom control FlashTrackBar
.
[C#] public class FlashTrackBar : RichControl { ... //private data member endColor private Color endColor = Color.LimeGreen; //Attributes for the property that tell the designer to display //it in the Flash grouping. [ Category("Flash"), Description("The ending color of the bar.") ] //public property EndColor that accesses endColor public Color EndColor { get { return endColor; } set { endColor = value; if (baseBackground != null && showGradient) { baseBackground.Dispose(); baseBackground = null; } //Invalidate calls the OnPaint method that redraws the control Invalidate(); } } ... }
The following code fragment associates a type converter and a UI type editor with the property Value
. In this case Value
is an integer and has a default type converter, but the idea is to display it as a percentage. The UI type editor,
allows the percentage to be displayed visually. This example also shows that the type converter or editor specified by the TypeConverter or Editor attribute overrides the default converter.FlashTrackBarValueConverter
,
[C#] [ Category("Flash"),TypeConverter(typeof(FlashTrackBarValueConverter)),
Editor(typeof(FlashTrackBarValueEditor), typeof(UITypeEditor)),
Description("The current value of the track bar. You may enter an actual value or a percentage.") ] public int Value {…}