C#封装WinForm控件遇到的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Windows.Forms.Design;
using System.Drawing.Design;
namespace CommonControl
{
public class MyListControl : Control
{
public MyListControl()
{
}
private MlyScope scopes = new MlyScope(0, 0);
[Browsable(true)]
public MlyScope Scopes
{
get { return scopes; }
set { scopes = value; }
}
}
[TypeConverter(typeof(MlyScopeConverter))]
public class MlyScope
{
private Int32 _min;
private Int32 _max;
public MlyScope()
{
}
public MlyScope(Int32 min, Int32 max)
{
_min = min;
_max = max;
}
[Browsable(true)]
public Int32 Min
{
get
{
return _min;
}
set
{
_min = value;
}
}
[Browsable(true)]
public Int32 Max
{
get
{
return _max;
}
set
{
_max = value;
}
}
}
public class MlyScopeConverter : 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 ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
String result = "";
if (destinationType == typeof(String))
{
MlyScope scope = (MlyScope)value;
result = scope.Min.ToString() + "," + scope.Max.ToString();
return result;
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(MlyScope).GetConstructor(new Type[] { typeof(Int32), typeof(Int32) });
MlyScope scope = (MlyScope)value;
return new InstanceDescriptor(ci, new object[] { scope.Min, scope.Max });
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
String[] v = ((String)value).Split(',');
if (v.GetLength(0) != 2)
{
throw new ArgumentException("Invalid parameter format");
}
MlyScope csf = new MlyScope();
csf.Min = Convert.ToInt32(v[0]);
csf.Max = Convert.ToInt32(v[1]);
return csf;
}
return base.ConvertFrom(context, culture, value);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(MlyScope), attributes);
}
}