实现PointF和SizeF的属性在PropertyGrid中可编辑的方法
2007-12-11 08:07
555 查看
在PropertyGrid中,PointF和SizeF类型的属性是只读的,我分析了DotNet中的Point的定义,编写了2个属性类,以此实现在PropertyGrid中可以象Point那样直接修改数据的功能。
PointF属性类的定义:
/**//// <summary>
/// PointFConverter 的摘要说明。
/// </summary>
public class PointFConverter : TypeConverter
...{
// Methods
public PointFConverter()
...{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
...{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
...{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
...{
if (!(value is string))
...{
return base.ConvertFrom(context, culture, value);
}
string text = ((string) value).Trim();
if (text.Length == 0)
...{
return null;
}
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
char ch = culture.TextInfo.ListSeparator[0];
string[] textArray = text.Split(new char[] ...{ ch });
float[] numArray = new float[textArray.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
for (int i = 0; i < numArray.Length; i++)
...{
numArray[i] = (float) converter.ConvertFromString(context, culture, textArray[i]);
}
if (numArray.Length != 2)
...{
throw new ArgumentException("格式不正确!");
}
return new PointF(numArray[0], numArray[1]);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
...{
if (destinationType == null)
...{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is PointF))
...{
PointF pointf = (PointF) value;
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
string[] textArray = new string[2];
int num = 0;
textArray[num++] = converter.ConvertToString(context, culture, pointf.X);
textArray[num++] = converter.ConvertToString(context, culture, pointf.Y );
return string.Join(separator, textArray);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is SizeF))
...{
PointF pointf2 = (PointF) value;
ConstructorInfo member = typeof(PointF).GetConstructor(new Type[] ...{ typeof(float), typeof(float) });
if (member != null)
...{
return new InstanceDescriptor(member, new object[] ...{ pointf2.X, pointf2.Y });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
...{
return new PointF((float) propertyValues["X"], (float) propertyValues["Y"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
...{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
...{
return TypeDescriptor.GetProperties(typeof(PointF), attributes).Sort(new string[] ...{ "X", "Y" });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
...{
return true;
}
}
SizeF属性类的定义:
/**//// <summary>
/// SizeFConverter 的摘要说明。
/// </summary>
public class SizeFConverter : TypeConverter
...{
// Methods
public SizeFConverter()
...{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
...{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
...{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
...{
if (!(value is string))
...{
return base.ConvertFrom(context, culture, value);
}
string text = ((string) value).Trim();
if (text.Length == 0)
...{
return null;
}
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
char ch = culture.TextInfo.ListSeparator[0];
string[] textArray = text.Split(new char[] ...{ ch });
float[] numArray = new float[textArray.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
for (int i = 0; i < numArray.Length; i++)
...{
numArray[i] = (float) converter.ConvertFromString(context, culture, textArray[i]);
}
if (numArray.Length != 2)
...{
throw new ArgumentException("格式不正确!");
}
return new SizeF(numArray[0], numArray[1]);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
...{
if (destinationType == null)
...{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is SizeF))
...{
SizeF size = (SizeF) value;
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
string[] textArray = new string[2];
int num = 0;
textArray[num++] = converter.ConvertToString(context, culture, size.Width);
textArray[num++] = converter.ConvertToString(context, culture, size.Height);
return string.Join(separator, textArray);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is SizeF))
...{
SizeF size2 = (SizeF) value;
ConstructorInfo member = typeof(SizeF).GetConstructor(new Type[] ...{ typeof(float), typeof(float) });
if (member != null)
...{
return new InstanceDescriptor(member, new object[] ...{ size2.Width, size2.Height });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
...{
return new SizeF((float) propertyValues["Width"], (float) propertyValues["Height"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
...{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
...{
return TypeDescriptor.GetProperties(typeof(SizeF), attributes).Sort(new string[] ...{ "Width", "Height" });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
...{
return true;
}
}
使用方法:
[Description("位置信息"),
CategoryAttribute ("1.基础信息"),
TypeConverterAttribute(typeof(PointFConverter))]
public PointF 位置
...{
get...{return base.位置;}
set...{base.位置=value;}
}
[Description("尺寸信息"),
CategoryAttribute ("1.基础信息"),
PropertyOrder(13),
BrowsableAttribute(false),
TypeConverterAttribute(typeof(SizeFConverter))]
public SizeF 尺寸
...{
get...{return base.尺寸;}
set...{base.尺寸=value;}
}
PointF属性类的定义:
/**//// <summary>
/// PointFConverter 的摘要说明。
/// </summary>
public class PointFConverter : TypeConverter
...{
// Methods
public PointFConverter()
...{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
...{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
...{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
...{
if (!(value is string))
...{
return base.ConvertFrom(context, culture, value);
}
string text = ((string) value).Trim();
if (text.Length == 0)
...{
return null;
}
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
char ch = culture.TextInfo.ListSeparator[0];
string[] textArray = text.Split(new char[] ...{ ch });
float[] numArray = new float[textArray.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
for (int i = 0; i < numArray.Length; i++)
...{
numArray[i] = (float) converter.ConvertFromString(context, culture, textArray[i]);
}
if (numArray.Length != 2)
...{
throw new ArgumentException("格式不正确!");
}
return new PointF(numArray[0], numArray[1]);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
...{
if (destinationType == null)
...{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is PointF))
...{
PointF pointf = (PointF) value;
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
string[] textArray = new string[2];
int num = 0;
textArray[num++] = converter.ConvertToString(context, culture, pointf.X);
textArray[num++] = converter.ConvertToString(context, culture, pointf.Y );
return string.Join(separator, textArray);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is SizeF))
...{
PointF pointf2 = (PointF) value;
ConstructorInfo member = typeof(PointF).GetConstructor(new Type[] ...{ typeof(float), typeof(float) });
if (member != null)
...{
return new InstanceDescriptor(member, new object[] ...{ pointf2.X, pointf2.Y });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
...{
return new PointF((float) propertyValues["X"], (float) propertyValues["Y"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
...{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
...{
return TypeDescriptor.GetProperties(typeof(PointF), attributes).Sort(new string[] ...{ "X", "Y" });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
...{
return true;
}
}
SizeF属性类的定义:
/**//// <summary>
/// SizeFConverter 的摘要说明。
/// </summary>
public class SizeFConverter : TypeConverter
...{
// Methods
public SizeFConverter()
...{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
...{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
...{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
...{
if (!(value is string))
...{
return base.ConvertFrom(context, culture, value);
}
string text = ((string) value).Trim();
if (text.Length == 0)
...{
return null;
}
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
char ch = culture.TextInfo.ListSeparator[0];
string[] textArray = text.Split(new char[] ...{ ch });
float[] numArray = new float[textArray.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
for (int i = 0; i < numArray.Length; i++)
...{
numArray[i] = (float) converter.ConvertFromString(context, culture, textArray[i]);
}
if (numArray.Length != 2)
...{
throw new ArgumentException("格式不正确!");
}
return new SizeF(numArray[0], numArray[1]);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
...{
if (destinationType == null)
...{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is SizeF))
...{
SizeF size = (SizeF) value;
if (culture == null)
...{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
string[] textArray = new string[2];
int num = 0;
textArray[num++] = converter.ConvertToString(context, culture, size.Width);
textArray[num++] = converter.ConvertToString(context, culture, size.Height);
return string.Join(separator, textArray);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is SizeF))
...{
SizeF size2 = (SizeF) value;
ConstructorInfo member = typeof(SizeF).GetConstructor(new Type[] ...{ typeof(float), typeof(float) });
if (member != null)
...{
return new InstanceDescriptor(member, new object[] ...{ size2.Width, size2.Height });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
...{
return new SizeF((float) propertyValues["Width"], (float) propertyValues["Height"]);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
...{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
...{
return TypeDescriptor.GetProperties(typeof(SizeF), attributes).Sort(new string[] ...{ "Width", "Height" });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
...{
return true;
}
}
使用方法:
[Description("位置信息"),
CategoryAttribute ("1.基础信息"),
TypeConverterAttribute(typeof(PointFConverter))]
public PointF 位置
...{
get...{return base.位置;}
set...{base.位置=value;}
}
[Description("尺寸信息"),
CategoryAttribute ("1.基础信息"),
PropertyOrder(13),
BrowsableAttribute(false),
TypeConverterAttribute(typeof(SizeFConverter))]
public SizeF 尺寸
...{
get...{return base.尺寸;}
set...{base.尺寸=value;}
}
相关文章推荐
- 用 PropertyGrid 实现 Visual Studio 2005 样式的属性窗口
- jQuery实现点击表格单元格就可以编辑内容的方法【测试可用】
- 不用ajax实现点击文字即可编辑的方法
- 用FlexGrid的EnterCell方法来实现控件可编辑
- Ruby实现的最短编辑距离计算方法
- JavaScript简单遍历DOM对象所有属性的实现方法
- 实现div可编辑的常见方法总结
- Unity获取object所有属性的一个方法,一些界面上没有开放的属性可以用该方法编辑
- Jquery中LigerUi的弹出编辑框(实现方法)
- Python实现动态添加类的属性或成员函数的解决方法
- js实现编辑div节点名称的方法
- Ajax - JavaScript之实现私有属性、像C++和Java一样支持基于类的继承方法之例子
- BeanUtil为javaBean注入属性支持类型 以及个人的实现方法
- 创建Employee类,在类中定义三个属性:编号,姓名,年龄,然后在构造方法里初始化这三个属性,最后载实现接口中的定义的CompareTo方法,将对象按编号升序排列。根据已有的代码,补全程序。
- 1 编写一个Book类,该类至少有name和price两个属性。该类要实现Comarable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。
- XAF中实现树形属性或树形编辑
- Javascript中prototype属性实现给内置对象添加新的方法
- 实现input元素中placeholder属性内容水平居中的方法
- 用JAVASCRIPT实现静态对象、静态方法和静态属性
- Python通过属性手段实现只允许调用一次的方法