Writing and. NET Properties Window Interactive Rad component (iii)

Source: Internet
Author: User
Tags bool list of attributes trim
Interaction six, extended Properties and string conversions: TypeConverter and Properties Window
. Net Property window is one of the most important features that can display nested properties, which provides a finer and more logical classification than attribute categories. Nested attributes are appropriate for both the class display and the sort display. This will make the list of attributes more compact. For example, it would be more reasonable to replace top and left two attributes with a location attribute with child attributes X and Y.




Figure 2. Nested properties





But how do you decide if a property can be expanded? These are not determined by the property window, but by the property's own type. In the. NET framework, each type is associated with a typeconverter. For example, the TypeConverter of Boolean and string will not allow expansion. Because it makes no sense to have a Boolean type containing child attributes.

In the. NET framework, TypeConverter actually executes a number of methods, more in the Properties window. As his name suggests, TypeConverter provides a dynamic standard way of changing from one type to another. In fact, the Properties window only deals with strings. So he relies on TypeConverter to convert between types (mostly and string conversions). TypeConverter can also provide extended performance and complex types to interact with the property window.

For example, look at the following person class:

[TypeConverter (typeof (Personconverter))]

public class Person

{

private string firstName = "";

private string lastName = "";

Private intage = 0;



public int Age

{

Get

{

return age;

}

Set

{

Age = value;

}

}





public string FirstName

{

Get

{

return firstName;

}

Set

{

This.firstname = value;

}

}





public string LastName

{

Get

{

return lastName;

}

Set

{

This.lastname = value;

}

}

}

We notice that the person class is assigned the TypeConverterAttribute attribute, and the TypeConverterAttribute attribute also specifies the type converter (Personconverter) of the class. If you do not specify the TypeConverterAttribute attribute, the TypeConverter class is used by default, and TypeConverter works well for some simple data types, such as font point, but if the data type is more complex, So it is possible that the conversion to the type is not what we want, so it is necessary to derive our own type converter from TypeConverter, here is personconverter, in this case, We first overload the getpropertiessupported and GetProperties methods to determine whether the property can be expanded.

Internal class Personconverter:typeconverter

{





public override PropertyDescriptorCollection

GetProperties (ITypeDescriptorContext context,

Object value,

Attribute[] Filter

{

return Typedescriptor.getproperties (value, filter);

}





public override bool Getpropertiessupported (

ITypeDescriptorContext context)

{

return true;

}

}

In general, it is sufficient to convert directly using Tpyeconverter. A simple extension is a direct derivation of the type converter you want from TypeConverter, and a more complex extension needs to derive the type converter from the Expandableobjectconverter. Now we modify Personconverter to convert a person class and display a string.

Internal class Personconverter:expandableobjectconverter

{





public override bool CanConvertFrom (

ITypeDescriptorContext context, Type T)

{





if (t = = typeof (String))

{

return true;

}

Return base. CanConvertFrom (context, t);

}





public override Object ConvertFrom (

ITypeDescriptorContext context,

CultureInfo info,

Object value)

{





if (value is string)

{

Try

{

string s = (string) value;

Parse the format "last, a (age)"

//

int comma = S.indexof (', ');

if (comma!=-1)

{

Now so we have the comma, get

The last name.

String last = s.substring (0, comma);

int paren = S.lastindexof (');

if (paren!=-1 && s.lastindexof (') ') = = s.length-1)

{

Pick up the name

String a = s.substring (comma + 1, paren-comma-1);

Get the Age

int age = Int32.Parse (

S.substring (Paren + 1,

s.length-paren-2));

Person p = new person ();

P.age = age;

P.lastname = Last. Trim ();

P.firstname = A. Trim ();

return p;

}

}

}

Catch {}

If we got this far, complain we

Couldn ' t parse the string

//

throw New ArgumentException (

"Can not Convert" + (string) value +

"' to type Person");



}

Return base. ConvertFrom (context, info, value);

}



public override Object ConvertTo (

ITypeDescriptorContext context,

CultureInfo Culture,

Object value,

Type desttype)

{

if (desttype = = typeof (String) && value is person)

{

Person P = (person) value;

Simply build the string as "last, A (age)"

return p.lastname + "," +

P.firstname + "(" + p.age.tostring () + ")";

}

Return base. ConvertTo (context, culture, value, desttype);

}

}

Now look at our person property after you specify the Personconverter type converter, you can expand and manipulate it in two ways: directly modifying and using child attributes.




Figure 3. Implementation of the expanded TypeConverter





To use the above code, we generate a UserControl and write the following code:

Private person P = new person ();

Public by person

{

Get

{

return p;

}

Set

{

THIS.P = value;

}

}


-----------------------------------

<<<<<<<<<<<< to Be Continued >>>>>>>>>>>>




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.