using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
namespace MyControlProperty
{ public partial class UserControl1 : System.Windows.Forms.TextBox
{
public UserControl1()
{
InitializeComponent();
}
XYProproperty xy = new XYProproperty(1, 2); public XYProproperty XY
{
get
{
return xy;
}
set
{
xy = value;
}
} }
[TypeConverter(typeof(XYConverter))]
public class XYProproperty
{
private double x;
private double y;
public XYProproperty()
{ }
public XYProproperty(double x, double y)
{
this.x = x;
this.y = y;
}
public double X
{
get
{
return x;
}
set
{
x = value;
}
}
public double Y
{
get
{
return y;
}
set
{
y = value;
}
}
}
public class XYConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext 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);
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string[] s = ((string)value).Split(new char[] { ',' });
if (s.Length != 2)
{
throw new ArgumentException("参数只能为两个!");
} try
{
XYProproperty xy = new XYProproperty(double.Parse(s[0]), double.Parse(s[1]));
return xy;
}
catch
{
throw new Exception("格式不正确!");
}
}
return base.ConvertFrom(context, culture, value);
} public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{ if (destinationType == typeof(string))
{
XYProproperty xy = (XYProproperty)value;
return xy.X + "," + xy.Y;
}
if (destinationType == typeof(InstanceDescriptor))
{
XYProproperty xy = (XYProproperty)value;
System.Reflection.ConstructorInfo constructor = typeof(XYProproperty).GetConstructor(new Type[] { typeof(double), typeof(double), typeof(double) });
if (constructor != null)
{ return new InstanceDescriptor(constructor, new object[] { xy.X, xy.Y }); }
} return base.ConvertTo(context, culture, value, destinationType);
} public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
} public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return new XYProproperty((double)propertyValues["X"], (double)propertyValues["Y"]);
} public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(XYProproperty), attributes).Sort(new string[] { "X", "Y" });
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
namespace MyControlProperty
{ public partial class UserControl1 : System.Windows.Forms.TextBox
{
public UserControl1()
{
InitializeComponent();
}
XYProproperty xy = new XYProproperty(1, 2); public XYProproperty XY
{
get
{
return xy;
}
set
{
xy = value;
}
} }
[TypeConverter(typeof(XYConverter))]
public class XYProproperty
{
private double x;
private double y;
public XYProproperty()
{ }
public XYProproperty(double x, double y)
{
this.x = x;
this.y = y;
}
public double X
{
get
{
return x;
}
set
{
x = value;
}
}
public double Y
{
get
{
return y;
}
set
{
y = value;
}
}
}
public class XYConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext 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);
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string[] s = ((string)value).Split(new char[] { ',' });
if (s.Length != 2)
{
throw new ArgumentException("参数只能为两个!");
} try
{
XYProproperty xy = new XYProproperty(double.Parse(s[0]), double.Parse(s[1]));
return xy;
}
catch
{
throw new Exception("格式不正确!");
}
}
return base.ConvertFrom(context, culture, value);
} public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{ if (destinationType == typeof(string))
{
XYProproperty xy = (XYProproperty)value;
return xy.X + "," + xy.Y;
}
if (destinationType == typeof(InstanceDescriptor))
{
XYProproperty xy = (XYProproperty)value;
System.Reflection.ConstructorInfo constructor = typeof(XYProproperty).GetConstructor(new Type[] { typeof(double), typeof(double), typeof(double) });
if (constructor != null)
{ return new InstanceDescriptor(constructor, new object[] { xy.X, xy.Y }); }
} return base.ConvertTo(context, culture, value, destinationType);
} public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
} public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return new XYProproperty((double)propertyValues["X"], (double)propertyValues["Y"]);
} public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(XYProproperty), attributes).Sort(new string[] { "X", "Y" });
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货