1、先画出用于下拉编辑器UI的用户控件
代码:
2、再编写属性值编辑器类
3、再添加一个属性,并用EditorAttribute 指定相应的编辑器类
The end.
代码:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace CustomControlSample
10{
11 [DesignTimeVisible(false)] //不显示在ToolBox中
12 public partial class UcSimpleCustomTypeDropDownEditor : UserControl
13 {
14 private SimpleCustomType _oldSCT;
15 private SimpleCustomType _newSCT;
16 private bool canceling = false; // 按Esc 键,关闭下拉菜单,不更新值。
17
18 public SimpleCustomType SCT
19 {
20 get { return _newSCT; }
21 }
22
23 public UcSimpleCustomTypeDropDownEditor(SimpleCustomType sct)
24 {
25 _oldSCT = sct;
26 _newSCT = sct;
27 InitializeComponent();
28 }
29
30 private void UcSimpleCustomTypeDropDownEditor_Load(object sender, EventArgs e)
31 {
32 this.textBox1.Text = _oldSCT.Min.ToString();
33 this.textBox2.Text = _oldSCT.Max.ToString();
34 }
35
36 private void textBox1_Validating(object sender, CancelEventArgs e)
37 {
38 try
39 {
40 int.Parse(textBox1.Text);
41 }
42 catch (FormatException)
43 {
44 e.Cancel = true;
45 MessageBox.Show("无效的值。", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
46 }
47 }
48
49 private void textBox2_Validating(object sender, CancelEventArgs e)
50 {
51 try
52 {
53 int.Parse(textBox2.Text);
54 }
55 catch (FormatException)
56 {
57 e.Cancel = true;
58 MessageBox.Show("无效的值。", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
59 }
60 }
61
62 protected override bool ProcessDialogKey(Keys keyData)
63 {
64 if (keyData == Keys.Escape)
65 {
66 canceling = true;
67 }
68 return base.ProcessDialogKey(keyData);
69 }
70
71 private void UcSimpleCustomTypeDropDownEditor_Leave(object sender, EventArgs e)
72 {
73 if (!canceling)
74 {
75 SCT.Min = int.Parse(this.textBox1.Text);
76 SCT.Max = int.Parse(this.textBox2.Text);
77 }
78 }
79 }
80}
81
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace CustomControlSample
10{
11 [DesignTimeVisible(false)] //不显示在ToolBox中
12 public partial class UcSimpleCustomTypeDropDownEditor : UserControl
13 {
14 private SimpleCustomType _oldSCT;
15 private SimpleCustomType _newSCT;
16 private bool canceling = false; // 按Esc 键,关闭下拉菜单,不更新值。
17
18 public SimpleCustomType SCT
19 {
20 get { return _newSCT; }
21 }
22
23 public UcSimpleCustomTypeDropDownEditor(SimpleCustomType sct)
24 {
25 _oldSCT = sct;
26 _newSCT = sct;
27 InitializeComponent();
28 }
29
30 private void UcSimpleCustomTypeDropDownEditor_Load(object sender, EventArgs e)
31 {
32 this.textBox1.Text = _oldSCT.Min.ToString();
33 this.textBox2.Text = _oldSCT.Max.ToString();
34 }
35
36 private void textBox1_Validating(object sender, CancelEventArgs e)
37 {
38 try
39 {
40 int.Parse(textBox1.Text);
41 }
42 catch (FormatException)
43 {
44 e.Cancel = true;
45 MessageBox.Show("无效的值。", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
46 }
47 }
48
49 private void textBox2_Validating(object sender, CancelEventArgs e)
50 {
51 try
52 {
53 int.Parse(textBox2.Text);
54 }
55 catch (FormatException)
56 {
57 e.Cancel = true;
58 MessageBox.Show("无效的值。", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
59 }
60 }
61
62 protected override bool ProcessDialogKey(Keys keyData)
63 {
64 if (keyData == Keys.Escape)
65 {
66 canceling = true;
67 }
68 return base.ProcessDialogKey(keyData);
69 }
70
71 private void UcSimpleCustomTypeDropDownEditor_Leave(object sender, EventArgs e)
72 {
73 if (!canceling)
74 {
75 SCT.Min = int.Parse(this.textBox1.Text);
76 SCT.Max = int.Parse(this.textBox2.Text);
77 }
78 }
79 }
80}
81
2、再编写属性值编辑器类
1SimpleCustomTypeDropDownEditor
3、再添加一个属性,并用EditorAttribute 指定相应的编辑器类
1下拉属性值编辑对话框的属性
The end.