WinForm Control Development Summary (7) -- provides editing functions for complex attributes

Source: Internet
Author: User
In the previous articles, we added a complex type Scope for the control and provided a type converter for its type. Now we can edit its value in the property browser, and its value is serialized in the source code. But have you found that it is not convenient to edit the attribute value in the property browser. Because the attribute is only "10, 200" Therefore, you must modify the format according to this format. If a format error occurs, an exception is thrown. For example, 10200" . We expect that each sub-attribute of this attribute can be independently edited. This is not impossible and the implementation is quite simple.
To independently edit sub-attributes in the property browser, We need to rewrite two methods: GetPropertiesSupported () and GetProperties (). The complete code of ScopeConverter is as follows:

Public class ScopeConverter: TypeConverter
{
Public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
{
If (sourceType = typeof (String) return true;

Return base. CanConvertFrom (context, sourceType );
}

Public override bool canconvertion (ITypeDescriptorContext context, Type destinationType)
{
If (destinationType = typeof (String) return true;

If (destinationType = typeof (InstanceDescriptor) return true;

Return base. canconvertize (context, destinationType );
}

Public override object convertion (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 ))
{
ConstructorInfo 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 base. convertize (context, culture, value, destinationType );
}

Public override object ConvertFrom (ITypeDescriptorContext context, System. Globalization. CultureInfo culture, object value)
{
If (value is string)
{
String [] v = (String) value). Split (',');
If (v. GetLength (0 )! = 2)
{
Throw new ArgumentException ("Invalid parameter format ");
}

Scope csf = new Scope ();
Csf. Min = Convert. ToInt32 (v [0]);
Csf. Max = Convert. ToInt32 (v [1]);
Return csf;
}
Return base. ConvertFrom (context, culture, value );
}

Public override bool GetPropertiesSupported (ITypeDescriptorContext context)
{
Return true;
}

Public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute [] attributes)
{
Return TypeDescriptor. GetProperties (typeof (Scope), attributes );
}
}

In the GetProperties method, I use TypeDescriptor to obtain all attribute descriptors of the Scope class and return them. If you are not familiar with TypeDescriptor, refer to MSDN.
After the two methods are rewritten and compiled, you can view the properties of the control in the test project. You can see that the Scope is in the following form:

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.