To write a RAD component that interacts with the. NET Properties window (vi)

Source: Internet
Author: User
Tags bool
Interactive again to the weekend, with so little of their own time, so the last did not make up the part of the whole, the body of the fifth article to the time, in fact, has been sent out, now to be issued is to honor what I said in the preface, The source of the type converter that you implement with TypeConverter is also sent out, there is no reason to be very clear in preface, if you look at the previous chapters and try to experiment, you will know this reason, I follow the method described in this paper constructs its own data type converter, The IDE's Properties window does also correctly expand and parse child properties. But when I modified the child properties, I found two problems, one is that the child attributes can not be modified (the specific reasons for this problem I forgot), and the second is by modifying the string of the parent property to make a mistake (as if the attribute type is wrong), So I had to do it myself. Derive a new converter from TypeConverter (original from Expandableobjectconverter), and overload most of the methods (introduced on MSDN), the most critical I think is the implementation of the ConvertFrom method, After constructing this converter, the property window works well, the two problems mentioned above are gone, and now it is not the problem of choosing the base class, the main thing is to construct it completely.

The following code is my implementation of the type conversion of pointf, if it is a custom type, constructed in exactly the same way, the most critical place in the overload is the implementation of the Getpropertys, can not directly return the base class method, otherwise, the value of the child property is not modified, You must return to TypeDescriptor's getpropertys, and for why, check out the relevant articles on MSDN for an introduction.

Implementation of transformation class for #region pointf
<summary>
Implementation of POINTF Transformation class
</summary>
Internal sealed class Pointfconverter:typeconverter
{

<summary>
CanConvertFrom method of heavy TypeConverter
</summary>
<param name= "Context" ></param>
<param name= "SourceType" ></param>
The target type to test
<returns></returns>
public override bool CanConvertFrom (ITypeDescriptorContext context,
Type sourcetype)
{
if (sourcetype = = typeof (String))//sourcetype is of type
{
return true;
}
Return base. CanConvertFrom (context, sourcetype);
}

<summary>
ConvertFrom method of heavy TypeConverter
Defines a conversion algorithm from the source type to the target type
</summary>
<param name= "Context" ></param>
<param name= "Culture" ></param>
Localization parameters
<param name= "Value" ></param>
Input string
<returns></returns>
public override Object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is String)//value is a type instance
{
String[] v = ((string) value). Split (new char[] {', '});
return new PointF (float. Parse (v[0]), float. Parse (v[1]));
}
Return base. ConvertFrom (context, culture, value);
}

<summary>
ConvertTo method of heavy TypeConverter
Defines an algorithm that converts a custom type to a target type
</summary>
<param name= "Context" ></param>
<param name= "Culture" ></param>
<param name= "Value" ></param>
<param name= "destinationtype" ></param>
Target type
<returns></returns>
public override Object ConvertTo (ITypeDescriptorContext Context,cultureinfo Culture, object value, Type destinationtype )
{
if (destinationtype = = typeof (String))//destinationtype is of type
{
Return ((PointF) value). X + "," + ((PointF) value). Y
}
Return base. ConvertTo (context, culture, value, destinationtype);
}

<summary>
Open expandable
Do not call base class virtual functions
</summary>
<param name= "Context" ></param>
<returns></returns>
public override bool Getpropertiessupported (ITypeDescriptorContext context)
{
return true;
}

<summary>
Get a list of child attributes
Returns the appropriate type resolution for using the TypeDescriptor object
Cannot call base class virtual function
</summary>
<param name= "Context" ></param>
<param name= "Value" ></param>
<param name= "Attributes" ></param>
<returns></returns>
public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, attribute[] Attributes
{
return typedescriptor.getproperties (value, attributes);
}

}
Implementation of transformation class for #endregion pointf


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

In this article, the description of how the default value of a property is not very detailed, you can see my own definition of the default value of component properties

Http://blog.csdn.net/zoulng/archive/2005/03/23/328342.aspx

<<<<<<<<<<<< End >>>>>>>>>>>>>>>



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.