Your clock control now encapsulates a Label control and a Timer component, each with its own set of inherent properties. While the individual properties of these controls will not be accessible to subsequent users of your control, you can create and expose custom properties by writing the appropriate blocks of code. In the following section, you will add properties to your control that enable the user to change the color of the background and text.
To add a Property to your user control
- In Solution Explorer, right-click ctlClock.cs, and then click View Code from the shortcut menu.
The Code Editor for your control opens.
- Locate the
public class ctlClockstatement. Beneath the opening{, type:private Color colFColor;
private Color colBColor;These statements create the private variables that you will use to store the values for the properties you are about to create.
- Type the following code beneath the variable declarations from step 2:
// Declares the name and type of the property.
public Color ClockBackColor
// Retrieves the value of the private variable colBColor.
{
get
{
return colBColor;
}
// Stores the selected value in the private variable colBColor, and
// updates the backcolor of the label control lblDisplay.
set
{
colBColor = value;
lblDisplay.BackColor = colBColor;
}
}
// Provides a similar set of instructions for the forecolor.
public Color ClockForeColor
{
get
{
return colFColor;
}
set
{
colFColor = value;
lblDisplay.ForeColor = colFColor;
}
}The preceding code makes two custom properties, ClockForeColor and ClockBackColor, available to subsequent users of this control. The get and set statements provide for storage and retrieval of the property value, as well as code to implement functionality appropriate to the property.
- From the File menu, choose Save All to save the project.


0 comments:
Post a Comment