Description of attributes (attribute) of the custom component (i)

Source: Internet
Author: User
of a custom component

Description of attributes (property)



Properties are an important part of the C # language, especially when we write our own components. I believe members must have a certain understanding of it. But have you noticed a very critical detail? That is, when you use any component, you need to assign a value to each attribute through the property browser, and it is more user-friendly for each of the different types of attributes to be their own form. For example, a numeric type, a string type, is the default form of a simple input, and a property such as a font, a color type can be a dialog box or a Drop-down list box. Do not know if you know how these are written? In fact, these functions are through the attributes (attribute) to achieve, the following I on this issue and everyone to learn, but before reading this article requires you to have a certain understanding of the property, good below us to the point.

In fact, the property and attribute translation into Chinese is the meaning of attributes, but in English there is a small difference. Because in Chinese, it is not good to distinguish property and attribute, so I temporarily translate these two words into attributes and properties, if there is anything wrong, please point out.

In C # not only attributes have their own nature, classes, methods, events and so have their own nature, due to my knowledge limit, so you can only introduce the nature of the property please forgive us.

Now you know the nature, but in C # What is the nature of it? Let me give you an example of one of the following WebService examples:

[WebMethod]

public string HelloWorld ()

{

Return "Hello World by Zhx";

}

This is an example of an external release method in WebService, where [WebMethod] is the attribute of this method. In WebService, if the method does not add this nature can not be published, I was the first time to write in C # WebService Use this property cause I thought it was my program error.

Here's an example of the nature of a property:



private int _value;



[DefaultValue (1)]

[Description (Value of text box)]

public int Value

{

Get

{

Return This._value

}



Set

{

This._value=value;

}



}

What do you find when you use these two properties [DefaultValue (1)], [Description ("The value of the text box")] to your own property? You can test it yourself. The nature of the attribute is mainly right. NET environment, the property editor plays a role in the UI, so you have to compile it before you write the code, and then refer to it in your form. OK, let me introduce to you the nature of what I know.

First from the simple to introduce to you:

1, CategoryAttribute Everyone from this name can see his role, his role is to define the attributes according to a certain type of classification, as we see in the system "appearance" and other categories.

2, DescriptionAttribute do not know if you remember that in the system after the election of a property, in the property browser, the property will appear below the text description. This is the nature of this function.

3, DefaultValueAttribute as the name implies that the nature of course is set the default value of the function, when the user developed a default value, the property browser will be bold font display.

4, ReadOnlyAttribute This nature is not difficult to see that he is set properties of the read-only, here is not the property of the true read-only, here only to indicate whether in the property browser can be rewritten in nature.

5, Browerableattribute This nature of the function is to indicate whether in the property browser can browse the property. There are some attributes that you do not want to rewrite during design time or with a property browser, and you can make a modification.

These properties can be used in isolation and can be used together, following is an example of the use of these properties:

private string _appver= "1.0";



[CategoryAttribute ("Custom editor"),

DefaultValueAttribute ("1.0"),

DescriptionAttribute ("Version information"),

ReadOnlyAttribute (True),

Browerableattribute (True)]

public string AppVer

{

get {return this._appver;}

set {This._appver=value;}

}

You can see the following image after compiling and selecting the Modified property. Where the red Oval is marked is the role of the DescriptionAttribute property, while the other properties of the UI action is displayed in the Red rectangular box content.




Do not know if everyone found in my property appver and yours is a little different? Everybody look carefully, right is in my property behind also a browse button, and click he will pop up a dialog box, display version information, we must want to know this function is made, we do not worry behind me to introduce you.

Do not know whether we have noticed that we use size, Font, color and other types as attributes when the property browser in what form to change the value of our properties? The following three screens.










As you can see, the three attributes can be grouped into the basic four types, the enum is the form of the Drop-down list box, the size is expanded, the font is the form of the pop-up form, and the color is the Drop-down UI form. But for the nature of the angle is divided into two types, the first two kinds of enum, size required by the nature of the property converter (TypeConverter), the latter two forms are required editor (UITypeEditor). Let me introduce the two properties separately.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.