1、先画出弹出对话框的windows窗体
代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace CustomControlSample
10

{
11
public partial class FrmSimpleCustomTypeDialogEditor : Form
12
{
13
private SimpleCustomType _simpleCustomType;
14
15
public SimpleCustomType SimpleCustomTypeProperty
16
{
17
get
{ return _simpleCustomType; }
18
set
{ _simpleCustomType = value; }
19
}
20
21
22
public FrmSimpleCustomTypeDialogEditor(SimpleCustomType sct)
23
{
24
InitializeComponent();
25
this._simpleCustomType = sct;
26
this.textBox1.Text = sct.Min.ToString();
27
this.textBox2.Text = sct.Max.ToString();
28
}
29
30
private void button1_Click(object sender, EventArgs e)
31
{
32
this.DialogResult = DialogResult.OK;
33
this._simpleCustomType.Min = int.Parse(this.textBox1.Text);
34
this._simpleCustomType.Max = int.Parse(this.textBox2.Text);
35
this.Close(); // 这句随便写不写?!!
36
}
37
38
private void button2_Click(object sender, EventArgs e)
39
{
40
this.Close();
41
}
42
43
private void textBox1_Validating(object sender, CancelEventArgs e)
44
{
45
try
46
{
47
int.Parse(textBox1.Text);
48
}
49
catch (FormatException)
50
{
51
e.Cancel = true;
52
MessageBox.Show("无效的值。", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
53
}
54
}
55
56
private void textBox2_Validating(object sender, CancelEventArgs e)
57
{
58
try
59
{
60
int.Parse(textBox2.Text);
61
}
62
catch (FormatException)
63
{
64
e.Cancel = true;
65
MessageBox.Show("无效的值。", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
66
}
67
}
68
69
}
70
}

2

3

4

5

6

7

8

9

10



11

12



13

14

15

16



17



18



19

20

21

22

23



24

25

26

27

28

29

30

31



32

33

34

35

36

37

38

39



40

41

42

43

44



45

46



47

48

49

50



51

52

53

54

55

56

57



58

59



60

61

62

63



64

65

66

67

68

69

70

2、再编写属性值编辑器类
1
SimpleCustomTypeDialogEdit#region SimpleCustomTypeDialogEdit
2
/**//// <summary>
3
/// SimpleCustomType 值编辑器 for Modal
4
/// </summary>
5
public class SimpleCustomTypeDialogEditor : UITypeEditor
6
{
7
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
8
{
9
if (context != null && context.Instance != null)
10
{
11
return UITypeEditorEditStyle.Modal;
12
}
13
return base.GetEditStyle(context);
14
}
15
16
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
17
{
18
System.Windows.Forms.Design.IWindowsFormsEditorService editorService = null;
19
if (context != null && context.Instance != null && provider != null)
20
{
21
editorService = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
22
if (editorService != null)
23
{
24
CustomControl control = (CustomControl)context.Instance;
25
FrmSimpleCustomTypeDialogEditor dlg = new FrmSimpleCustomTypeDialogEditor(control.DialogProperty);
26
if (dlg.ShowDialog() == DialogResult.OK)
27
{
28
value = dlg.SimpleCustomTypeProperty;
29
return value;
30
}
31
}
32
}
33
34
return value;
35
}
36
}
37
#endregion


2


3

4

5

6



7

8



9

10



11

12

13

14

15

16

17



18

19

20



21

22

23



24

25

26

27



28

29

30

31

32

33

34

35

36

37

3、再添加一个属性,并用EditorAttribute 指定相应的编辑器类
1
弹出属性值编辑对话框的属性#region 弹出属性值编辑对话框的属性
2
// 注意:这里设置了复杂属性的初始值 new SimpleCustomType(1, 2)
3
private SimpleCustomType dialogField = new SimpleCustomType(1, 2);
4
5
[Category("弹出式属性值编辑对话框")]
6
[TypeConverter(typeof(SimpleCustomTypeConverter))]
7
[Editor(typeof(SimpleCustomTypeDialogEditor), typeof(UITypeEditor))]
8
public SimpleCustomType DialogProperty
9
{
10
get
{ return dialogField; }
11
set
{ dialogField = value; }
12
}
13
#endregion


2

3

4

5

6

7

8

9



10



11



12

13

The end.