拜服各位大虾,帮帮忙解决下这问题。。这是自定义控件的代码using System.ComponentModel;
using System.Windows.Forms;namespace WinForm
{
public partial class NumTextBox : TextBox
{
private static readonly NumberScope defaultNum = new NumberScope(0, 100); private NumberScope numRange = defaultNum; [Category("Vaildation")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public NumberScope NumRange
{
get { return numRange; }
set { numRange = value; }
} public void ResetNumRange()
{
numRange = defaultNum;
} public bool ShouleSerializeNumRange()
{
return !NumRange.Equals(defaultNum);
} [Browsable(false)]
public bool ContentsInRange
{
get { return NumRange.Validate(Text); }
} public NumTextBox()
{
InitializeComponent();
}
} [TypeConverter(typeof(NumberScopeConvertor))]
public class NumberScope
{
private double low;
public double Low
{
get { return low; }
set { low = value; }
} private double high;
public double High
{
get { return high; }
set { high = value;}
} public NumberScope(double low, double high)
{
this.high = high;
this.low = low;
} public bool Validate(string num)
{
double tmp = double.Parse(num);
if (tmp < high && tmp > low)
return true;
else
return false;
} public override string ToString()
{
return string.Format("{0},{1}", Low, High);
}
}
} public class NumberScopeConvertor:TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string[] numStr = ((string)value).Split(new char[] { ',' });
if (numStr.Length != 2)
{
throw new Exception("invalid format value");
}
else
{
try
{
double low = double.Parse(numStr[0]);
double high = double.Parse(numStr[1]);
return new NumberScope(low, high);
}
catch (FormatException)
{
throw new FormatException("format value to NumberScope faild");
}
}
}
return base.ConvertFrom(context, culture, value);
} 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, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ((NumberScope)value).ToString();
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(NumberScope).GetConstructor(new Type[] { typeof(double), typeof(double) });
NumberScope scope = (NumberScope)value;
return new InstanceDescriptor(ci, new object[] { scope.Low, scope.High });
}
return base.ConvertTo(context, culture, value, destinationType);
} public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection propertys = TypeDescriptor.GetProperties(typeof(NumberScope), attributes);
return propertys;
}
}之后在一个form上使用这个控件,打开这个控件的属性修改NumRange属性,Ctrl+S保存的时候会报错
报错内容为:
Code generation for property 'NumRange' failed. Error was: '[A]WinForm.NumberScope cannot be cast to [B]WinForm.NumberScope. Type A originates from 'WinForm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Users\CHUNSIBO\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\elgwjxaa01\WinForm.exe'. Type B originates from 'WinForm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Users\ss\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\ko-pdm2_01\WinForm.exe'.'
using System.Windows.Forms;namespace WinForm
{
public partial class NumTextBox : TextBox
{
private static readonly NumberScope defaultNum = new NumberScope(0, 100); private NumberScope numRange = defaultNum; [Category("Vaildation")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public NumberScope NumRange
{
get { return numRange; }
set { numRange = value; }
} public void ResetNumRange()
{
numRange = defaultNum;
} public bool ShouleSerializeNumRange()
{
return !NumRange.Equals(defaultNum);
} [Browsable(false)]
public bool ContentsInRange
{
get { return NumRange.Validate(Text); }
} public NumTextBox()
{
InitializeComponent();
}
} [TypeConverter(typeof(NumberScopeConvertor))]
public class NumberScope
{
private double low;
public double Low
{
get { return low; }
set { low = value; }
} private double high;
public double High
{
get { return high; }
set { high = value;}
} public NumberScope(double low, double high)
{
this.high = high;
this.low = low;
} public bool Validate(string num)
{
double tmp = double.Parse(num);
if (tmp < high && tmp > low)
return true;
else
return false;
} public override string ToString()
{
return string.Format("{0},{1}", Low, High);
}
}
} public class NumberScopeConvertor:TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string[] numStr = ((string)value).Split(new char[] { ',' });
if (numStr.Length != 2)
{
throw new Exception("invalid format value");
}
else
{
try
{
double low = double.Parse(numStr[0]);
double high = double.Parse(numStr[1]);
return new NumberScope(low, high);
}
catch (FormatException)
{
throw new FormatException("format value to NumberScope faild");
}
}
}
return base.ConvertFrom(context, culture, value);
} 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, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ((NumberScope)value).ToString();
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(NumberScope).GetConstructor(new Type[] { typeof(double), typeof(double) });
NumberScope scope = (NumberScope)value;
return new InstanceDescriptor(ci, new object[] { scope.Low, scope.High });
}
return base.ConvertTo(context, culture, value, destinationType);
} public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection propertys = TypeDescriptor.GetProperties(typeof(NumberScope), attributes);
return propertys;
}
}之后在一个form上使用这个控件,打开这个控件的属性修改NumRange属性,Ctrl+S保存的时候会报错
报错内容为:
Code generation for property 'NumRange' failed. Error was: '[A]WinForm.NumberScope cannot be cast to [B]WinForm.NumberScope. Type A originates from 'WinForm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Users\CHUNSIBO\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\elgwjxaa01\WinForm.exe'. Type B originates from 'WinForm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Users\ss\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\ko-pdm2_01\WinForm.exe'.'
C:\Users\ss\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\ko-pdm2_01\WinForm.exe怎么会有这两个,当前有几个项目, 把无关的去掉, 再次重新编译, 关闭, 再次加载看看,
你写的没问题。