自定义控件 属性扩展 多次“重新生成” 后 发生错误。环境:V.C#2005下载附件。打开工程后,选择在Form1。在工具箱中选取TestComboBox控件。
放置Form1任何位置。选择放置后的testComboBox1控件。打开属性面板。查看属性最顶端。
编辑属性组后 宽高都为1后。在Form1.Designer.cs原代码中会增加:
this.testComboBox1.TestSize = new ClassTestSize(1, 1);
“重新生成解决方案”多次后,会导致testComboBox1控件的自定义属性发生错误。第一次生成:
根属性字符不显示了。
第二次生成:
属性值都变成0了。
而且在设置任何一个值的时候,会出现这样的错误:
类型“ClassTestSize”的对象无法转换为类型“ClassTestSize”。附件:http://dl2.csdn.net/down4/20080317/17175753503.rar
放置Form1任何位置。选择放置后的testComboBox1控件。打开属性面板。查看属性最顶端。
编辑属性组后 宽高都为1后。在Form1.Designer.cs原代码中会增加:
this.testComboBox1.TestSize = new ClassTestSize(1, 1);
“重新生成解决方案”多次后,会导致testComboBox1控件的自定义属性发生错误。第一次生成:
根属性字符不显示了。
第二次生成:
属性值都变成0了。
而且在设置任何一个值的时候,会出现这样的错误:
类型“ClassTestSize”的对象无法转换为类型“ClassTestSize”。附件:http://dl2.csdn.net/down4/20080317/17175753503.rar
解决方案 »
- 用了backgroundWorker之后 timer不管用了
- .net 多文件上传
- 如何让asp.net的TreeView控件展开事件(TreeNodeExpand)只触发一次
- winforms程序如何只让一个实例运行呢?
- 小问题:冠以listbox中的个别语句不明白
- socket接收数据一定要写在一个死循环里吗?这样占用CPU太高了.有不有其他的什么方法?
- DataGrid内按钮单击事件不执行?
- C#初学者。求帮忙!
- 求C#模仿这个checkbox 旁边带着文字
- C# 可以像VBA那样开发office 应用吗??
- ASP.NET计算按钮的点击次数
- 水晶报表的问题,导出报表时(Execl)时导出的页面一下就闪过去了,请问原因??(抱歉没有分了)
放置Form1任何位置。选择放置后的testComboBox1控件。打开属性面板。查看属性最顶端。
编辑属性组后 宽高都为1后。在Form1.Designer.cs原代码中会增加:
this.testComboBox1.TestSize = new ClassTestSize(1, 1);
“重新生成解决方案”多次后,会导致testComboBox1控件的自定义属性发生错误。第一次生成:
根属性字符不显示了。
第二次生成:
属性值都变成0了。
而且在设置任何一个值的时候,会出现这样的错误:
类型“ClassTestSize”的对象无法转换为类型“ClassTestSize”。附件:http://dl2.csdn.net/down4/20080317/17175753503.rar
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Collections;[TypeConverter(typeof(ClassTestSizeTypeConverter))]
public class ClassTestSize {
private Int32 AttrWidth;
private Int32 AttrHeight;
public ClassTestSize() {
}
public ClassTestSize(Int32 DataWidth, Int32 DataHeight) {
Width = DataWidth;
Height = DataHeight;
}
[DefaultValueAttribute(0), Description("实验宽")]
public Int32 Width {
get { return AttrWidth; }
set { AttrWidth = value; }
}
[DefaultValueAttribute(0), Description("实验宽")]
public Int32 Height {
get { return AttrHeight; }
set { AttrHeight = value; }
}
}
public class ClassTestSizeTypeConverter : TypeConverter {
public override Boolean CanConvertFrom(ITypeDescriptorContext DataText, Type DataType) {
if (DataType == typeof(String)) {
return true;
}
return base.CanConvertFrom(DataText, DataType);
}
public override Object ConvertFrom(ITypeDescriptorContext DataText, CultureInfo DataCI, Object DataValue) {
if (DataValue is String) {
String[] HanText = ((String)DataValue).Split(',');
if (HanText.GetLength(0) != 2) {
throw new ArgumentException("长度不正确");
}
ClassTestSize DataObj = new ClassTestSize();
DataObj.Width = int.Parse(HanText[0]);
DataObj.Height = int.Parse(HanText[1]);
return DataObj;
}
return base.ConvertFrom(DataText, DataCI, DataValue);
} public override Boolean CanConvertTo(ITypeDescriptorContext DataText, Type DataType) {
if (DataType == typeof(String)) {
return true;
}
if (DataType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(DataText, DataType);
}
public override Object ConvertTo(ITypeDescriptorContext DataText, CultureInfo DataCI, Object DataValue, Type DataType) {
if (DataType == typeof(String)) {
ClassTestSize HanObj = (ClassTestSize)DataValue;
return (Object)(HanObj.Width.ToString() + "," + HanObj.Height.ToString());
}
if (DataType == typeof(InstanceDescriptor)) {
ClassTestSize HanObj = (ClassTestSize)DataValue;
ConstructorInfo HanCI = typeof(ClassTestSize).GetConstructor(new Type[] { typeof(Int32), typeof(Int32) });
return (Object)(new InstanceDescriptor(HanCI, new object[] { HanObj.Width, HanObj.Height }));
}
return base.ConvertTo(DataText, DataCI, DataValue, DataType);
} public override Boolean GetPropertiesSupported(ITypeDescriptorContext DataText) {
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext DataText, Object DataValue, Attribute[] Attributes) {
PropertyDescriptorCollection HanProperties = TypeDescriptor.GetProperties(DataValue);
return HanProperties;
} public override Boolean GetCreateInstanceSupported(ITypeDescriptorContext DataText) {
return true;
}
public override Object CreateInstance(ITypeDescriptorContext DataText, IDictionary PropertyValues) {
return new ClassTestSize((Int32)PropertyValues["Width"], (Int32)PropertyValues["Height"]);
}
}
public class TestComboBox : ComboBox {
[CategoryAttribute(" 自定义属性组1")]
[DisplayName(" TestSize")]
[TypeConverter(typeof(ClassTestSize))]
public ClassTestSize TestSize {
get { return AttrTestSize; }
set { AttrTestSize = value; }
}
public ClassTestSize AttrTestSize = new ClassTestSize();
}