自定义 Property Editor
有时候vs中自带的 Property Editor 不能满足我们的程序开发,这时自定义 Property Editor就显得非常重要了
一般显示 Property Editor 有2中方法:
1.通过 IWindowsFormsEditorService.ShowDialog( Form dialog),以弹出窗体的形式显示出来
2.通过IWindowsFormsEditorService.DropDownControl( Control control);以下拉列表的形式显示出来
自定义 Property Editor 是非常简单的,就是 新建一个Form 或 继承一个UserControl,但更重要的问题是
控件的属性与Property Editor 的交互问题,即:我怎么把 属性 以 自定义的Property Editor 这样的形式表现出来,还有怎么把 在 Property Editor 上的修改交互给属性呢
穿插一句:
有时候,定义的属性的类型并不是vs的基本类型,而是以Class或别的形式出现,如
这时,和这样的一个属性交互,就会涉及到 类型转换(TypeConverter)了
那么,今天就来和大家一起试图解决以上2问题:属性与Property Editor 的交互,TypeConverter
最终效果图:
以窗体的形式显示
(图A)
以下拉的形式出现:(图B)
自定义一个UserControl,作为下拉列表出现
(图C) ItemEditorControl.cs
定义一个窗体(即图A)
下载源代码
一般显示 Property Editor 有2中方法:
1.通过 IWindowsFormsEditorService.ShowDialog( Form dialog),以弹出窗体的形式显示出来
2.通过IWindowsFormsEditorService.DropDownControl( Control control);以下拉列表的形式显示出来
自定义 Property Editor 是非常简单的,就是 新建一个Form 或 继承一个UserControl,但更重要的问题是
控件的属性与Property Editor 的交互问题,即:我怎么把 属性 以 自定义的Property Editor 这样的形式表现出来,还有怎么把 在 Property Editor 上的修改交互给属性呢
穿插一句:
有时候,定义的属性的类型并不是vs的基本类型,而是以Class或别的形式出现,如
1 public Size _Size
2 {
3
4 get {
5 return size;
6 }
7 set {
8
9 size = value;
10 }
11 }
2 {
3
4 get {
5 return size;
6 }
7 set {
8
9 size = value;
10 }
11 }
这时,和这样的一个属性交互,就会涉及到 类型转换(TypeConverter)了
那么,今天就来和大家一起试图解决以上2问题:属性与Property Editor 的交互,TypeConverter
最终效果图:
以窗体的形式显示
(图A)
以下拉的形式出现:(图B)
1internal class ItemEditor : UITypeEditor
2 {
3
4 Methods..
87
88 }
2 {
3
4 Methods..
87
88 }
1class DropDownEditor : UITypeEditor
2 {
3 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
4 {
5 if (context != null && context.Instance != null)
6 {
7 return UITypeEditorEditStyle.DropDown;
8 }
9
10 return base.GetEditStyle(context);
11 }
12
13 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
14 {
15 IWindowsFormsEditorService editorService = null;
16
17 if (context != null && context.Instance != null && provider != null)
18 {
19
20 editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
21 Size tempSize = null;
22 if (context.Instance is ListBox)
23 tempSize = ((ListBox)context.Instance)._Size;
24 ItemEditorControl editorControl = new ItemEditorControl(tempSize);
25
26 editorService.DropDownControl(editorControl);
27 value = tempSize;
28 return value;
29
30 }
31 else
32 {
33 return null;
34 }
35
36 }
37
38 }
2 {
3 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
4 {
5 if (context != null && context.Instance != null)
6 {
7 return UITypeEditorEditStyle.DropDown;
8 }
9
10 return base.GetEditStyle(context);
11 }
12
13 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
14 {
15 IWindowsFormsEditorService editorService = null;
16
17 if (context != null && context.Instance != null && provider != null)
18 {
19
20 editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
21 Size tempSize = null;
22 if (context.Instance is ListBox)
23 tempSize = ((ListBox)context.Instance)._Size;
24 ItemEditorControl editorControl = new ItemEditorControl(tempSize);
25
26 editorService.DropDownControl(editorControl);
27 value = tempSize;
28 return value;
29
30 }
31 else
32 {
33 return null;
34 }
35
36 }
37
38 }
自定义一个UserControl,作为下拉列表出现
(图C) ItemEditorControl.cs
1public partial class ItemEditorControl : UserControl
2 {
3
4 private Size size;
5
6
7
8 属性
24
25
26
27 public ItemEditorControl(Size _size)
28 {
29 this.size = _size;
30 InitializeComponent();
31 }
32
33
34 public ItemEditorControl()
35 {
36 InitializeComponent();
37 }
38
39 private void ItemEditorControl_Load(object sender, EventArgs e)
40 {
41 if (size != null)
42 {
43 this.txtText.Text = size.Width.ToString();
44 this.txtValue.Text = size.Height.ToString();
45 }
46 }
47
48
49
50 private void ItemEditorControl_Leave(object sender, EventArgs e)
51 {
52 if (!String.IsNullOrEmpty(this.txtText.Text) && !String.IsNullOrEmpty(this.txtValue.Text))
53 {
54
55 if (size != null)
56 {
57 size.Width = Convert.ToInt16(this.txtText.Text);
58 size.Height = Convert.ToInt16(this.txtValue.Text);
59 }
60 else
61 size = new Size(Convert.ToInt16(txtText.Text), Convert.ToInt16(txtValue.Text));
62 }
63 }
64 }
2 {
3
4 private Size size;
5
6
7
8 属性
24
25
26
27 public ItemEditorControl(Size _size)
28 {
29 this.size = _size;
30 InitializeComponent();
31 }
32
33
34 public ItemEditorControl()
35 {
36 InitializeComponent();
37 }
38
39 private void ItemEditorControl_Load(object sender, EventArgs e)
40 {
41 if (size != null)
42 {
43 this.txtText.Text = size.Width.ToString();
44 this.txtValue.Text = size.Height.ToString();
45 }
46 }
47
48
49
50 private void ItemEditorControl_Leave(object sender, EventArgs e)
51 {
52 if (!String.IsNullOrEmpty(this.txtText.Text) && !String.IsNullOrEmpty(this.txtValue.Text))
53 {
54
55 if (size != null)
56 {
57 size.Width = Convert.ToInt16(this.txtText.Text);
58 size.Height = Convert.ToInt16(this.txtValue.Text);
59 }
60 else
61 size = new Size(Convert.ToInt16(txtText.Text), Convert.ToInt16(txtValue.Text));
62 }
63 }
64 }
定义一个窗体(即图A)
1 public partial class ItemEditorForm :Form
2 {
3 private ListItemCollection listItems;
4 private string temp="";
5 public ItemEditorForm(ListItemCollection _listItem)
6 {
7 this.listItems = _listItem;
8
9 InitializeComponent();
10 }
11
12 private void OK_Click(object sender, EventArgs e)
13 {
14
15 if (listItems != null)
16 listItems.Clear();
17 string delimStr = ",";
18 char [] delimiter = delimStr.ToCharArray();
19 string [] split = null;
20 string b = this.textBox1.Text;
21
22 split = b.Split(delimiter);
23 if (split.Length > 0)
24 {
25 for (int i = 0; i < split.Length; )
26 {
27 ListItem listItem = new ListItem(split[i], split[i + 1]);
28
29 listItems.Add(listItem);
30 i += 2;
31 }
32 }
33 else
34 {
35 listItems = null;
36 }
37
38 DialogResult = DialogResult.OK;
39 Close();
40 }
41
42 private void ItemEditorForm_FormClosing(object sender, FormClosingEventArgs e)
43 {
44
45 }
46
47 private void ItemEditorForm_Load(object sender, EventArgs e)
48 {
49
50
51 if (listItems!=null && listItems.Count>0)
52 {
53 foreach (ListItem listItem in listItems)
54 temp += listItem.Text + "," + listItem.Value + ",";
55 this.textBox1.Text = temp.Substring(0, temp.Length - 1);
56 }
57 }
58 }
2 {
3 private ListItemCollection listItems;
4 private string temp="";
5 public ItemEditorForm(ListItemCollection _listItem)
6 {
7 this.listItems = _listItem;
8
9 InitializeComponent();
10 }
11
12 private void OK_Click(object sender, EventArgs e)
13 {
14
15 if (listItems != null)
16 listItems.Clear();
17 string delimStr = ",";
18 char [] delimiter = delimStr.ToCharArray();
19 string [] split = null;
20 string b = this.textBox1.Text;
21
22 split = b.Split(delimiter);
23 if (split.Length > 0)
24 {
25 for (int i = 0; i < split.Length; )
26 {
27 ListItem listItem = new ListItem(split[i], split[i + 1]);
28
29 listItems.Add(listItem);
30 i += 2;
31 }
32 }
33 else
34 {
35 listItems = null;
36 }
37
38 DialogResult = DialogResult.OK;
39 Close();
40 }
41
42 private void ItemEditorForm_FormClosing(object sender, FormClosingEventArgs e)
43 {
44
45 }
46
47 private void ItemEditorForm_Load(object sender, EventArgs e)
48 {
49
50
51 if (listItems!=null && listItems.Count>0)
52 {
53 foreach (ListItem listItem in listItems)
54 temp += listItem.Text + "," + listItem.Value + ",";
55 this.textBox1.Text = temp.Substring(0, temp.Length - 1);
56 }
57 }
58 }
下载源代码