WinForm Control Development Summary (vii) Provide editing for child properties of complex properties

Source: Internet
Author: User
Tags bool min tostring

In the previous articles, we added a complex type scope to the control and a type converter for its type, now we can edit its value in the property browser, and its value is also serialized in the source code. But have you found that it is not convenient to edit the value of this property in the property browser. Because the attribute is only "10,200" this form, so you have to follow this format to modify, once the format error will throw an exception, such as entering a "10200". We expect that every child of this attribute will be able to be edited independently, which is not impossible, and is simple to implement.

In order to edit the child properties independently in the property browser, we also rewrite two methods: Getpropertiessupported () and GetProperties (), and the following is the complete code for Scopeconverter:

     public class Scopeconverter:typeconverter {public override bool CanConvertFrom (Itypedescriptorconte


            XT context, Type sourcetype) {if (sourcetype = = typeof (String)) return true; Return base.

        CanConvertFrom (context, sourcetype);

            public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationtype) {


            if (destinationtype = = typeof (String)) return true;


            if (destinationtype = = typeof (InstanceDescriptor)) return true; Return base.

        CanConvertTo (context, destinationtype); Override Object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, O

            Bject 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 = ty Peof (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);  Override Object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value {if (value is String) {string[] v = ((string) value). S

                Plit (', '); 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 to CSF; Return to base.

        ConvertFrom (context, culture, value); public override bool Getpropertiessupported (ITypeDescriptorContext context) {return Tru

        E Override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Att

        Ribute[] attributes) {return typedescriptor.getproperties (typeof (Scope), attributes); }

}

In the GetProperties method, I used TypeDescriptor to get all the property descriptors for the scope class and return it. If you are not familiar with TypeDescriptor, you can refer to MSDN.

After rewriting the two methods and compiling the properties of the control in the test project, you can see that scope is 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.