111,094
社区成员




this.testComboBox1.TestSize = new ClassTestSize(1, 1);
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Collections;
[TypeConverter(typeof(ClassTestSizeTypeConverter))]
public class ClassTestSize {
private Int32 AttrWidth;
private Int32 AttrHeight;
public ClassTestSize() {
}
public ClassTestSize(Int32 DataWidth, Int32 DataHeight) {
Width = DataWidth;
Height = DataHeight;
}
[DefaultValueAttribute(0), Description("实验宽")]
public Int32 Width {
get { return AttrWidth; }
set { AttrWidth = value; }
}
[DefaultValueAttribute(0), Description("实验宽")]
public Int32 Height {
get { return AttrHeight; }
set { AttrHeight = value; }
}
}
public class ClassTestSizeTypeConverter : TypeConverter {
public override Boolean CanConvertFrom(ITypeDescriptorContext DataText, Type DataType) {
if (DataType == typeof(String)) {
return true;
}
return base.CanConvertFrom(DataText, DataType);
}
public override Object ConvertFrom(ITypeDescriptorContext DataText, CultureInfo DataCI, Object DataValue) {
if (DataValue is String) {
String[] HanText = ((String)DataValue).Split(',');
if (HanText.GetLength(0) != 2) {
throw new ArgumentException("长度不正确");
}
ClassTestSize DataObj = new ClassTestSize();
DataObj.Width = int.Parse(HanText[0]);
DataObj.Height = int.Parse(HanText[1]);
return DataObj;
}
return base.ConvertFrom(DataText, DataCI, DataValue);
}
public override Boolean CanConvertTo(ITypeDescriptorContext DataText, Type DataType) {
if (DataType == typeof(String)) {
return true;
}
if (DataType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(DataText, DataType);
}
public override Object ConvertTo(ITypeDescriptorContext DataText, CultureInfo DataCI, Object DataValue, Type DataType) {
if (DataType == typeof(String)) {
ClassTestSize HanObj = (ClassTestSize)DataValue;
return (Object)(HanObj.Width.ToString() + "," + HanObj.Height.ToString());
}
if (DataType == typeof(InstanceDescriptor)) {
ClassTestSize HanObj = (ClassTestSize)DataValue;
ConstructorInfo HanCI = typeof(ClassTestSize).GetConstructor(new Type[] { typeof(Int32), typeof(Int32) });
return (Object)(new InstanceDescriptor(HanCI, new object[] { HanObj.Width, HanObj.Height }));
}
return base.ConvertTo(DataText, DataCI, DataValue, DataType);
}
public override Boolean GetPropertiesSupported(ITypeDescriptorContext DataText) {
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext DataText, Object DataValue, Attribute[] Attributes) {
PropertyDescriptorCollection HanProperties = TypeDescriptor.GetProperties(DataValue);
return HanProperties;
}
public override Boolean GetCreateInstanceSupported(ITypeDescriptorContext DataText) {
return true;
}
public override Object CreateInstance(ITypeDescriptorContext DataText, IDictionary PropertyValues) {
return new ClassTestSize((Int32)PropertyValues["Width"], (Int32)PropertyValues["Height"]);
}
}
public class TestComboBox : ComboBox {
[CategoryAttribute(" 自定义属性组1")]
[DisplayName(" TestSize")]
[TypeConverter(typeof(ClassTestSize))]
public ClassTestSize TestSize {
get { return AttrTestSize; }
set { AttrTestSize = value; }
}
public ClassTestSize AttrTestSize = new ClassTestSize();
}
this.testComboBox1.TestSize = new ClassTestSize(1, 1);