Customize components
Property Nature (Attribute)
Property as an important part of the C # language, especially when we write components, it is more important. I believe that everyone must have a certain understanding. But if you pay attention to a very key detail problem? That is, when you use any of the parts, you need to assign each property through the property browser, and more friendly is your own form for each different type of properties. For example: digital type, string type is the form of a default simple input, such as font, the property of the Color type, can dialog or drop-down list box. I don't know if everyone knows how these is written? In fact, these functions are implemented through the property (attribute), I will learn from this issue and everyone, but when reading this article, I ask everyone to have a certain understanding of the property, so we are justified by our collapse.
In fact, Property and Attribute translate into Chinese are meaningful, but there is a small difference in English. Because it is not good to distinguish Property and Attribute in Chinese, I temporarily translate these two words into properties (Attribute), if anything is wrong, please point out.
In C # Not only attributes have their own nature, classes, methods, events, etc. have their own nature. Because of my knowledge, I can only introduce you to the nature of the property. Please forgive.
Now everyone knows the nature, but what is the nature in C #? Let's take an example to tell you, please see the following WebService:
[WebMethod]
Public String HelloWorld ()
{
Return "Hello World By ENX";
}
This is an example of a foreign release method in WebService, where [WebMethod] is attribute of this method. In WebService If the method does not add this nature, it is not possible to publish it. I used to write WebService for the first time. When I write WebService, I used this nature that caused me to think that it was the error of my program.
Let's take a look at the nature of an attribute:
Private int _Value;
[DefaultValue (1)]]
[Description ("Value of Text Box")]
Public Int Value
{
get
{
Return this._Value
}
set
{
THIS._VALUE = VALUE;
}
}
Everyone will find these two properties [DefaultValue (1)], [Description ("The Value of Text Box"] What will I find out? Everyone can test themselves. The properties of the property are mainly role in the UI in the .NET environment, so everyone must compile it when writing completed code, and then quote in the form, you can set this property. effect. Ok, let me introduce you to the nature of myself.
First introduce you to you first:
1. CategoryAttribute can see his role from this name. His role is to classify the defined properties, as if you have the classification of "appearance" and the like seen in the system.
2, DescriptionAttribute I don't know if you still remember after a property in the system, the text description of this property will appear below the property browser. This nature has played such a function.
3. DEFAULTVALUEATTRIBUTE As the name suggests this nature is of course the feature of setting the default value. When the user has set the default value, the property browser will display the bold font. 4. ReadonlyAttribute is not difficult to see that he is a read-only property that sets properties. The reader here is not a real readableness of the attribute. Here is just whether it can be rewritten in the property browser.
5. BrowRableAttribute This nature function indicates whether you can browse this property in the property browser. There are some attributes that do not want to be rewritten during design or with the attribute browser, you can make changes.
These properties can not only be used alone, but also can be used together, the following example uses an example of using these properties:
Private string _appver = "1.0";
[CategoryAttribute ("Custom Editor"),
DefaultValueAttribute ("1.0"),
DESCRIPTIONATTRIBUTE ("Version Information"),
ReadonlyAttribute (TRUE),
Browrablettribute (TRUE)]
Public String Appver
{
Get {return this._appver;}
Set {this._appver = value;
}
After compiling, you can see the following screens after you select changes. Among them, the red elliptical type is the role of DescriptionAttribute, and the UI effect obtained by other properties is the content shown in the red rectangle frame.
I don't know if everyone finds a little different from my property appver and you? Let's take a closer look, it's a browsing button behind my properties, and click He will pop up a dialog, display the version information, everyone must know that this feature is made, don't I am in a hurry, I am introducing you.
I don't know if you have noticed it. How do you change our property in what type of property browser uses size, font, color and other types? The three screens below.
Everyone can see that these three properties can be divided into basic four types. Enum is the form of the drop-down list box. Size is the form of expansion, and font is in the form of a pop-up form. . However, the angle of the nature is divided into two types. The top two kinds of ENUM, SIZE need are attribute converters, and the latter two forms are required for an editor. Below I will introduce these two properties.