In the previous sections, we demonstrated how to develop user controls. You may still be confused about the basic settings of some attributes in the user controls, in this section, we will learn about the basic property settings of user controls.
The so-called basic attribute settings mean that the control can set attributes in the property designer. Some attributes have default values in the design, and some in the property designer will pop up an editor, value.
This section
1. Simple data type attribute settings
2. attribute settings for complex data types
1. Simple data type attribute settings
Simple data types are common types in. NET, such as string, integer, floating type, and these basic data types.
Let's take a look at these basic attributes.
Browsable: indicates whether the property can be displayed in the property designer. If it is TRUE, it is displayed. If it is FALSE, it is not displayed.
Category: indicates the Category of an attribute.
Description: The Description of the property.
DefaultValue: the default value of the property.
Below we define a user control and customize a property SecondText. The Code is as follows:
[Csharp]
Public partial class UCLabel: UserControl
{
Public UCLabel ()
{
InitializeComponent ();
}
Private string _ sText = "Helloworld"; // Initial Value
/// <Summary>
/// Browsable indicates whether the property is displayed in the designer. TRUE indicates that the property is displayed.
/// </Summary>
/// Description indicates the Description of the attribute.
/// Category indicates the Category of this attribute in the property designer.
[Browsable (true)]
[Category ("text")]
[Description ("second text")]
[DefaultValue ("Sina Weibo")] // Default Value
Public string SecondText
{
Get
{
Return _ sText;
}
Set
{
_ SText = value;
}
}
}
Public partial class UCLabel: UserControl
{
Public UCLabel ()
{
InitializeComponent ();
}
Private string _ sText = "Helloworld"; // Initial Value
/// <Summary>
/// Browsable indicates whether the property is displayed in the designer. TRUE indicates that the property is displayed.
/// </Summary>
/// Description indicates the Description of the attribute.
/// Category indicates the Category of this attribute in the property designer.
[Browsable (true)]
[Category ("text")]
[Description ("second text")]
[DefaultValue ("Sina Weibo")] // Default Value
Public string SecondText
{
Get
{
Return _ sText;
}
Set
{
_ SText = value;
}
}
}
From the above picture, we can see the corresponding items of each attribute in the property designer.
Here, we will set the initial value of SecondText to HelloWorld and the default value to Sina Weibo. Let's talk about the difference between the initial value and the default value.
1. The initial value is the value assigned when the attribute is defined. When the user control is dragged onto the form, the initial value is displayed by default.
2. The default value of an attribute is non-bold, and other values are bold.
3. To restore the default value of a property, right-click the property and choose reset to restore the default value of the property.
2. Basic settings of complex attributes
Author: zx13525079024