前几篇文章我们一直在讨论如何更方便的编辑复杂类型的属性,在这个过程中我介绍了类型转换器以及如何制作自己的类型转换器来实现属性值的串行化和实现子属性的编辑。对于Scope这种级别的复杂属性,一个类型转换器就已经足够了,但是对于更为复杂的属性,单单使用类型转换器已经不足以应付了,比如我们常用的Font属性。
在这种情况下,我们就需要提供更为复杂的编辑方式,比如属性编辑对话框,你还记得Font对话框吗?现在我们就来看看如何实现更复杂的属性编辑。复杂的属性编辑器分为两种类型,一种是弹出式模态对话框属性编辑器,一种式下拉式属性编辑器。如果你还没有感性的认识的话,可以观察一下TextBox控件的属性,Font属性的编辑器是模态对话框属性编辑器,Dock属性的编辑器是下拉式属性编辑器。
接下来我们来制作一个模态对话框编辑器,虽然Scope属性并不复杂,但是为了演示的方便,我们还是用它来做例子。
首先我们要做一个用来编辑属性的对话框,在对话框的构造函数里传入要编辑的属性的值。在对话框类里,声明一个Scope类型的私有变量_scope用以保存传入和编辑后的值。还要增加一个Scope属性,以便外部环境能够获取编辑后的结果。对话框的外观如下:
在这个对话框里,我们要把OK按钮的DialogResult属性设为OK(当点击OK按钮时,模态对话框关闭,并返回DialogResult.OK),将Cancel按钮的DialogResult属性设为Cancel(当点击OK按钮时,模态对话框关闭,并返回DialogResult.OK)。另外我们要对用户输入的值做验证,以保证Scope的min和max值都是Int32类型。下边是对话框的代码:
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 ScopeEditorDialog : Form
12 {
13 private Scope _scope = null;
14
15 public ScopeEditorDialog(Scope scope)
16 {
17 InitializeComponent();
18 _scope = scope;
19
20 textBox1.Text = _scope.Min.ToString();
21 textBox2.Text = _scope.Max.ToString();
22 }
23
24 private void button1_Click(object sender, EventArgs e)
25 {
26 _scope.Min = Convert.ToInt32(textBox1.Text);
27 _scope.Max = Convert.ToInt32(textBox2.Text);
28 }
29
30 private void textBox1_Validating(object sender, CancelEventArgs e)
31 {
32 try
33 {
34 Int32.Parse(textBox1.Text);
35
36 }
37 catch (FormatException)
38 {
39 e.Cancel = true;
40 MessageBox.Show("无效的值", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
41 }
42 }
43
44 private void textBox2_Validating(object sender, CancelEventArgs e)
45 {
46 try
47 {
48 Int32.Parse(textBox2.Text);
49 }
50 catch (FormatException)
51 {
52 e.Cancel = true;
53 MessageBox.Show("无效的值", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
54 }
55 }
56
57 public Scope Scope
58 {
59 get
60 {
61 return _scope;
62 }
63 set
64 {
65 _scope = value;
66 }
67 }
68 }
69 }
每一个属性的编辑器都是直接或者间接的派生于UITypeEditor。开发环境从来也不会直接调用我们编写的模态对话框来编辑属性,而是调用UITypeEditor的某些虚方法,所以我们还必须提供一个派生于UITypeEditor的类来与开发环境通信。下边的代码实现了Scope的编辑器:
1 using System;
2 using System.ComponentModel;
3 using System.Drawing.Design;
4 using System.Windows.Forms.Design;
5 using System.Windows.Forms;
6
7 namespace CustomControlSample
8 {
9 public class ScopeEditor:UITypeEditor
10 {
11 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
12 {
13 if (context != null && context.Instance != null)
14 {
15 return UITypeEditorEditStyle.Modal;
16 }
17
18 return base.GetEditStyle(context);
19 }
20
21 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
22 {
23 IWindowsFormsEditorService editorService = null;
24
25 if (context != null && context.Instance != null && provider != null)
26 {
27 editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
28 if (editorService != null)
29 {
30 MyListControl control = (MyListControl)context.Instance;
31 ScopeEditorDialog dlg = new ScopeEditorDialog(control.Scope);
32 if (dlg.ShowDialog()== DialogResult.OK)
33 {
34 value = dlg.Scope;
35 return value;
36 }
37 }
38 }
39
40 return value;
41 }
42 }
43 }
1 [Browsable(true)]
2 [Editor(typeof(ScopeEditor),typeof(UITypeEditor))]
3 public Scope Scope
4 {
5 get
6 {
7 return _scope;
8 }
9 set
10 {
11 _scope = value;
12 }
13 }
浙公网安备 33010602011771号