WinForm Control Development Summary (vi) control property type converter code detailed

Source: Internet
Author: User
Tags min

In the previous article, I added a complex property to the control and wrote a type converter for the type of the property, now let's look at the code for the type converter and explain the meaning of the code.

To implement a type converter, we have to rewrite (override) Four methods:

CanConvertFrom ()--tests based on the type parameters to determine whether it can be converted from this type to the current type, in this case we only provide the ability to convert string and InstanceDescriptor types.

CanConvertTo ()-tests based on the type parameters to determine whether it can be converted from the current type to the specified type.

ConvertTo ()-Converts the value of the parameter value to the specified type.

ConvertFrom ()-Swop parameter value and returns an object of the book type.

public override Object ConvertTo (ITypeDescriptorContext context, 

System.Globalization.CultureInfo culture,            Object value, Type destinationtype)

{

String result = ' ";

if (destinationtype = = typeof (String))

{

Scope scope = (scope) value;

result = scope. Min.tostring () + "," + scope.                Max.tostring ();

return result;


}


if (destinationtype = = typeof (InstanceDescriptor))

{

Constructori Nfo ci = typeof (Scope).                GetConstructor (new type[] 

{typeof (Int32), typeof (Int32)});

Scope scope = (scope) value;

return new InstanceDescriptor (CI, new object[] { 

scope. Min,scope.            Max});

Return to

base.        ConvertTo (context, culture, value, destinationtype);

}

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.