VS自带的控件大多数据,属性都是有默认值的. 这是怎么实现的. (有人说: 给字段设个初始值呗!)
疑点一: 默认值与初始值是等同的吗? 好像是哦?
VS中的默认值与初始值是等同的吗? 不知道? (实际上是不等同的.)
从MSDN上可以查到,简单属性与复杂属性的默认值设置又是不一样的.
属性的初始值与默认值不是一个概念, 大家可以很容易地看出来.
1, 初始值就是当控件第一次被拖到窗体上属性中的值. 对于简单的类型比如int类型, 初始值默认为0, 这是控件类被实例化时分配的.
2, 默认值是当属性值不为粗体时的那个值就是该属性的默认值.
来段代码先, (下面是综合了前几篇文章中所提及的代码) 只包含两个属性的控件,一个简单属性,一个简单属性
1
using System.ComponentModel;
2
using System.Windows.Forms;
3
using System.Drawing;
4
5
namespace CustomControlSample
6
{
7
public class CustomControl : Control
8
{
9
// 注意:这里设置了简单属性的初始值123
10
private int simpleField = 123;
11
12
[Category("我是属性,我怕谁!")]
13
[Description("我是属性,故我在(属性浏览器中)!")]
14
[DefaultValue(456)] // 如果属性 (Property) 具有简单的默认值,则应用 DefaultValueAttribute
15
public int SimpleProperty
16
{
17
get { return simpleField; }
18
set { simpleField = value; }
19
}
20
21
// 注意:这里设置了复杂属性的初始值 new SimpleCustomType(1, 2)
22
private SimpleCustomType complexField = new SimpleCustomType(1, 2);
23
24
[Category("我是复杂的属性哦!")]
25
[Description("我是简单的复杂属性,因为我是由简单的类型和简单的方式定义的。\n定义我的类型很简单,只有两个属性(Min, Max);定义我的body也很简单,只是简单的get, set.")]
26
[TypeConverter(typeof(SimpleCustomTypeConverter))]
27
public SimpleCustomType ComplexProperty
28
{
29
get { return complexField; }
30
set { complexField = value; }
31
}
32
33
// 如果属性不具有简单的默认值,则可以为属性提供可选方法 ShouldSerialize 和 Reset。
34
/// <summary>
35
/// 设置复杂属性的默认值 new SimpleCustomType(8, 9)
36
/// </summary>
37
public void ResetComplexProperty()
38
{
39
ComplexProperty = new SimpleCustomType(8, 9);
40
}
41
42
/// <summary>
43
/// 诸如 Visual Studio 之类的设计器可使用 ShouldSerializePropertyName 方法来检查属性是否已不再使用其默认值,
44
/// 并仅在属性发生改变的情况下将代码写入到窗体中,从而更有效地生成代码。
45
/// </summary>
46
/// <returns></returns>
47
public bool ShouldSerializeComplexProperty()
48
{
49
return (ComplexProperty.Min != 8 || ComplexProperty.Max != 9);
50
}
51
52
protected override void OnPaint(PaintEventArgs e)
53
{
54
base.OnPaint(e);
55
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
56
}
57
}
58
59
简单的自定义类型
88
89
简单的自定义类型的类型转换器
140
}
141
using System.ComponentModel;2
using System.Windows.Forms;3
using System.Drawing;4

5
namespace CustomControlSample6
{7
public class CustomControl : Control8
{9
// 注意:这里设置了简单属性的初始值12310
private int simpleField = 123;11

12
[Category("我是属性,我怕谁!")]13
[Description("我是属性,故我在(属性浏览器中)!")]14
[DefaultValue(456)] // 如果属性 (Property) 具有简单的默认值,则应用 DefaultValueAttribute15
public int SimpleProperty16
{17
get { return simpleField; }18
set { simpleField = value; }19
}20

21
// 注意:这里设置了复杂属性的初始值 new SimpleCustomType(1, 2)22
private SimpleCustomType complexField = new SimpleCustomType(1, 2);23

24
[Category("我是复杂的属性哦!")]25
[Description("我是简单的复杂属性,因为我是由简单的类型和简单的方式定义的。\n定义我的类型很简单,只有两个属性(Min, Max);定义我的body也很简单,只是简单的get, set.")]26
[TypeConverter(typeof(SimpleCustomTypeConverter))]27
public SimpleCustomType ComplexProperty28
{29
get { return complexField; }30
set { complexField = value; }31
}32

33
// 如果属性不具有简单的默认值,则可以为属性提供可选方法 ShouldSerialize 和 Reset。34
/// <summary>35
/// 设置复杂属性的默认值 new SimpleCustomType(8, 9)36
/// </summary>37
public void ResetComplexProperty()38
{39
ComplexProperty = new SimpleCustomType(8, 9);40
}41

42
/// <summary>43
/// 诸如 Visual Studio 之类的设计器可使用 ShouldSerializePropertyName 方法来检查属性是否已不再使用其默认值,44
/// 并仅在属性发生改变的情况下将代码写入到窗体中,从而更有效地生成代码。45
/// </summary>46
/// <returns></returns>47
public bool ShouldSerializeComplexProperty()48
{49
return (ComplexProperty.Min != 8 || ComplexProperty.Max != 9);50
}51

52
protected override void OnPaint(PaintEventArgs e)53
{54
base.OnPaint(e);55
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));56
}57
}58

59
简单的自定义类型88

89
简单的自定义类型的类型转换器140
}141

强烈建议有需要的同学仔细阅读代码中的注释部分.并在本机上反复实践.
The end.


浙公网安备 33010602011771号