读书人

C#封装WinForm控件遇到的有关问题

发布时间: 2013-03-28 10:20:24 作者: rapoo

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);
}
}


当在属性面板上填写数字的时候
C#封装WinForm控件遇到的有关问题
报错了
C#封装WinForm控件遇到的有关问题
当更改此类库的版本号后好了,但是再次将这个控件拖入到Form控件上点保存时又出现了这个问题! c# winform
[解决办法]
有两个版本的CommonControl
[解决办法]
问题的答案在提示中已经给你说了。
同时可以参见我的商业控件库(全源码)
地址为:
http://item.taobao.com/item.htm?spm=a1z10.1.w8679133417.6.zwE4C8&id=18719359053

读书人网 >.NET Framework

热点推荐