Description of attributes (property) of custom Component (ii)
Source: Internet
Author: User
One: Property converter (TypeConverter)
1, drop-down list box in the form of:
To use a property of the form of a drop-down list box we first define a property, in this case I define a string-type property FileName.
private string _filename;
public string FileName
{
get {return this._filename;}
set {This._filename=value;}
}
After defining the properties, we also have a property converter of our own. So what is a property converter? In fact, only the string type is recognized in the property browser, so we want to convert our properties to strings through the property converter, and then convert the string to our own property after the property browser changes the string. Does everyone sound a bit of a smear? Okay, let's do a property converter. We'll see.
Because the attribute in this example is a string type, we are going to inherit a new string-form property Converter from System.ComponentModel.StringConverter. Here is the code and the code in the comments, I believe you will be able to read:
<summary>
The converter that expands the string (the style that implements the Drop-down list box)
</summary>
public class FileNameConverter:System.ComponentModel.StringConverter
{
<summary>
Determines whether a Drop-down box is supported based on the return value
</summary>
<returns>
True: The form of a down box
False: The form of normal text editing
</returns>
public override bool GetStandardValuesSupported (System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
<summary>
What's specific in the dropdown box
</summary>
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues ( System.ComponentModel.ITypeDescriptorContext context)
{
return new StandardValuesCollection (new string[]{"File1.bat", "File2.exe", "File3.dll"});
}
<summary>
Determines whether a text box is not editable based on the return value
</summary>
<returns>
True: text boxes cannot be edited
Flase: Text boxes can be edited
</returns>
public override bool GetStandardValuesExclusive (System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
OK, the property converter is finished, and finally don't forget to assign this property converter to the property we just wrote. Oh, the code is as follows:
[CategoryAttribute (custom type Converter) for complex type settings,
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