什么叫类型转换器?
前篇文章中的为何没有用到, 其实它也用到了, 因为它用的是系统自带的类型int, 类型转换器已经由系统自动提供了.
如果我们使用了自己定义的类型, 因为系统中没有相应的类型转换器, 这就需要我们写一个类型转换器.
下面我们写一个稍稍复杂点的属性, 它是由简单的类型加简单的属性组合而成的,(没有晕吧),
也就是说我要自已定义一个类型, 而不用系统自带的类型(比如前篇文章中的int类型)
下面就是拥有一个简单的复杂属性的简单控件,![](https://www.cnblogs.com/Emoticons/QQ/smile.gif)
编译, 拖到windows窗体上,点击查看属性浏览器,
![](https://www.cnblogs.com/images/cnblogs_com/luqingfei/SimpleComplexProperty_1.gif)
wow, 是灰色的,不能使用. 为啥?
..... 那是因为属性浏览器不知道如何转换我的属性,
我们不是写了类型转换器了吗? 没有被使用, ...
又要用到Attribute了,这真是个好东西呀
在上面的代码中的属性ComplexProperty 用TypeConverter (TypeConverterAttribute的缩写)指定一下我们自定义的类型的类型转换器即可.
[TypeConverter(typeof(SimpleCustomTypeConverter))]
public SimpleCustomType ComplexProperty
{
get { return complexField; }
set { complexField = value; }
}
再编译......查看属性浏览器
![](https://www.cnblogs.com/images/cnblogs_com/luqingfei/SimpleComplexProperty_2.gif)
OK了
The end.
前篇文章中的为何没有用到, 其实它也用到了, 因为它用的是系统自带的类型int, 类型转换器已经由系统自动提供了.
如果我们使用了自己定义的类型, 因为系统中没有相应的类型转换器, 这就需要我们写一个类型转换器.
下面我们写一个稍稍复杂点的属性, 它是由简单的类型加简单的属性组合而成的,(没有晕吧),
也就是说我要自已定义一个类型, 而不用系统自带的类型(比如前篇文章中的int类型)
下面就是拥有一个简单的复杂属性的简单控件,
![](https://www.cnblogs.com/Emoticons/QQ/smile.gif)
1
using System.ComponentModel;
2
using System.Windows.Forms;
3
using System.Drawing;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace CustomControlSample
6
{
7
public class SimpleComplexProperty : Control
8
{
9
private SimpleCustomType complexField;
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
[Category("我是复杂的属性哦!")]
12
[Description("我是简单的复杂属性,因为我是由简单的类型和简单的方式定义的。\n定义我的类型很简单,只有两个属性(Min, Max);定义我的body也很简单,只是简单的get, set.")]
13
public SimpleCustomType ComplexProperty
14
{
15
get { return complexField; }
16
set { complexField = value; }
17
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
protected override void OnPaint(PaintEventArgs e)
20
{
21
base.OnPaint(e);
22
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
简单的自定义类型
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
简单的自定义类型的类型转换器
108
}
109![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
编译, 拖到windows窗体上,点击查看属性浏览器,
![](https://www.cnblogs.com/images/cnblogs_com/luqingfei/SimpleComplexProperty_1.gif)
wow, 是灰色的,不能使用. 为啥?
..... 那是因为属性浏览器不知道如何转换我的属性,
我们不是写了类型转换器了吗? 没有被使用, ...
又要用到Attribute了,这真是个好东西呀
在上面的代码中的属性ComplexProperty 用TypeConverter (TypeConverterAttribute的缩写)指定一下我们自定义的类型的类型转换器即可.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
再编译......查看属性浏览器
![](https://www.cnblogs.com/images/cnblogs_com/luqingfei/SimpleComplexProperty_2.gif)
OK了
The end.