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: