Architecture and Design of Windows Forms Custom Controls—Building Custom Controls with Default Property Values
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| .NET Articles |
| © 2006 Eric White |
Building Custom Controls with Default Property Values
In the CustomControlWithProperties example, all the properties were initialized with the usual default values for variables—a blank string, zero, a null font, or a null color. In most cases, it is better to have properties initialized to some meaningful values by default. This makes your control easy for other developers to use.
There are two aspects to using default property values. You need to make sure that your properties are initialized in the code for the control, and also that Visual Studio .NET understands the default values. The latter point is important because Visual Studio .NET, when aware of default values, is able to take several steps to make editing the properties easier:
* If the value of the property is not the default value, Visual Studio .NET will display the value of the property in bold. For example, the default value for the Text property for a form or control is the empty string. If you set this property to anything else, it will be displayed in bold.
* Visual Studio .NET will provide a context menu for the property that allows the programmer using the control to reset the value to the default value. The programmer is able to right click the property value and pick Reset Value from the context menu.
* Visual Studio .NET can generate slightly more efficient code.
You can see this behavior for the properties of the forms and controls that are part of the Windows Forms framework.
You must take two steps to initialize your properties to default values and have Visual Studio .NET process these values properly: initialize your member variables and set an attribute on the property. You’ll see how this works in the next example.
|

