我先描述一下我的问题,我的自定义控件有几个属性(比如P1,P2,P3),我想把它们放到一起,前面有个小加号的那种,就像有些控件中的Font,Padding,Size一样,那应该将这几个属性放在结构中还是类中?我开始放在结构中,
public struct StructType
{
public StructType(int v1,int v2,int v3):this()
{
...
}
public int P1{get;set;}
public int P2get;set;}
public int P3{get;set;}
}
然后在自定义控件中声明了一个属性PPP.
有关转换器、编辑器也写完并应用了。知道那出了问题,设计的时时候从属性窗口设置了三个属性的值却保留不下来,一运行就成了最初的默认值,后来在结构上应用了SrializableAttribute定制属性,这下可以倒是可以了,但在设计器自动为窗体生成的代码中对于这部分生成的代码中成了:
<自定义控件实例>.PPP=(StructType)resources.GetObjectData("...");
也就是说没有调用我在结构中定义的构造函数,怎么才能像控件的Padding属性一样让设计器调用构造函数呢?(问题未完,请先解答一下吧?视解答情况可能还会有后续问题拿出来请教!分不够再加,谢谢先!)
public struct StructType
{
public StructType(int v1,int v2,int v3):this()
{
...
}
public int P1{get;set;}
public int P2get;set;}
public int P3{get;set;}
}
然后在自定义控件中声明了一个属性PPP.
有关转换器、编辑器也写完并应用了。知道那出了问题,设计的时时候从属性窗口设置了三个属性的值却保留不下来,一运行就成了最初的默认值,后来在结构上应用了SrializableAttribute定制属性,这下可以倒是可以了,但在设计器自动为窗体生成的代码中对于这部分生成的代码中成了:
<自定义控件实例>.PPP=(StructType)resources.GetObjectData("...");
也就是说没有调用我在结构中定义的构造函数,怎么才能像控件的Padding属性一样让设计器调用构造函数呢?(问题未完,请先解答一下吧?视解答情况可能还会有后续问题拿出来请教!分不够再加,谢谢先!)
解决方案 »
- 今天使用C#做一个下载文件的程序 发现了两种可以下载文件的方法,请高手指点下两种的区别
- WinForm中自定义控件里面的Label随控件大小的变化而变化
- 为什么会找不到radiobuttonlist呢
- 在windows中使用monodevelop开发的程序还需要在客户机上安装.NET Framework吗?
- datatable 显示的问题
- 请各位大大帮忙解决下一个泛型类的问题
- 各位大大,帮忙看看这个多线程问题~
- 公司不让用QQ,MSN,,,能不能自己写个聊天工具啊(只要2个人可以聊的就可以了)?
- 使用OpenFileDialog打开文件时,如何实现一次选定多个文件?
- 帮小弟解决下关于datagrid的问题,小弟刚入门,急~~~~~~~~~~~~~~~
- winform 和 webform 之间通信
- dataset datatable
public struct StructType
{
public StructType(int v1, int v2, int v3)
{
_p1 = v1;
_p2 = v2;
_p3 = v3;
}
public int P1
{
get { return _p1; }
set { _p1 = value; }
}int _p1; public int P2
{
get { return _p2; }
set { _p2 = value; }
} int _p2; public int P3
{
get { return _p3; }
set { _p3 = value; }
}int _p3;
}
定制的转换器(TypeConverter) class ssConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) ||
destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(typeof(StructType).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int) }),
new object[] { ((StructType)value).P1, ((StructType)value).P2, ((StructType)value).P3 });
}
if (destinationType == typeof(string))
{
string s = ((StructType)value).P1 + "," + ((StructType)value).P2 + "," + ((StructType)value).P3;
return s;
}
return base.ConvertTo(context, culture, value, destinationType);
}
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, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
string[] cs = ((string)value).Split(',');
StructType v = default(StructType);
int i;
if (int.TryParse(cs[0], out i))
v.P1 = i;
if (int.TryParse(cs[1], out i))
v.P2 = i;
if (int.TryParse(cs[2], out i))
v.P3 = i;
return v;
}
return base.ConvertFrom(context, culture, value);
}
}
给某个控件或者窗口增加一个属性: public StructType V
{
get { return _v; }
set { _v = value; }
} StructType _v;设计器生成的代码: this.folderViewTree1.V = new XXXX.Windows.Forms.StructType(2, 3, 5);
这样运行时就很 OK 了.
public class MultiStatusImage : System.ComponentModel.ExpandableObjectConverter
{
。
}
+个MultiStatusImageTypeConvert.cs
public class MultiStatusImageTypeConvert : TypeConverter
{
public MultiStatusImageTypeConvert()
{
//
// TODO: Add constructor logic here
//
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(MultiStatusImage));
}
}
你的方法有机会我会试一下。