Dot Net WinForm 控件开发 (六) 为属性提供弹出式编辑对话框

本文介绍如何使用C#创建自定义属性编辑器,通过弹出对话框来编辑复杂类型的属性值。具体包括窗体代码编写、编辑器类实现及属性配置。

1、先画出弹出对话框的windows窗体


代码如下:

 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Data;
 5None.gifusing System.Drawing;
 6None.gifusing System.Text;
 7None.gifusing System.Windows.Forms;
 8None.gif
 9None.gifnamespace CustomControlSample
10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
11InBlock.gif    public partial class FrmSimpleCustomTypeDialogEditor : Form
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        private SimpleCustomType _simpleCustomType;
14InBlock.gif
15InBlock.gif        public SimpleCustomType SimpleCustomTypeProperty
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _simpleCustomType; }
18ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _simpleCustomType = value; }
19ExpandedSubBlockEnd.gif        }

20InBlock.gif
21InBlock.gif
22InBlock.gif        public FrmSimpleCustomTypeDialogEditor(SimpleCustomType sct)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            InitializeComponent();
25InBlock.gif            this._simpleCustomType = sct;
26InBlock.gif            this.textBox1.Text = sct.Min.ToString();
27InBlock.gif            this.textBox2.Text = sct.Max.ToString();
28ExpandedSubBlockEnd.gif        }

29InBlock.gif
30InBlock.gif        private void button1_Click(object sender, EventArgs e)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            this.DialogResult = DialogResult.OK;
33InBlock.gif            this._simpleCustomType.Min = int.Parse(this.textBox1.Text);
34InBlock.gif            this._simpleCustomType.Max = int.Parse(this.textBox2.Text);
35InBlock.gif            this.Close(); // 这句随便写不写?!!
36ExpandedSubBlockEnd.gif        }

37InBlock.gif
38InBlock.gif        private void button2_Click(object sender, EventArgs e)
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40InBlock.gif            this.Close();
41ExpandedSubBlockEnd.gif        }

42InBlock.gif
43InBlock.gif        private void textBox1_Validating(object sender, CancelEventArgs e)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            try
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                int.Parse(textBox1.Text);
48ExpandedSubBlockEnd.gif            }

49InBlock.gif            catch (FormatException)
50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
51InBlock.gif                e.Cancel = true;
52InBlock.gif                MessageBox.Show("无效的值。""验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
53ExpandedSubBlockEnd.gif            }

54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif        private void textBox2_Validating(object sender, CancelEventArgs e)
57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
58InBlock.gif            try
59ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
60InBlock.gif                int.Parse(textBox2.Text);
61ExpandedSubBlockEnd.gif            }

62InBlock.gif            catch (FormatException)
63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
64InBlock.gif                e.Cancel = true;
65InBlock.gif                MessageBox.Show("无效的值。""验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
66ExpandedSubBlockEnd.gif            }

67ExpandedSubBlockEnd.gif        }

68InBlock.gif
69ExpandedSubBlockEnd.gif    }

70ExpandedBlockEnd.gif}


2、再编写属性值编辑器类

 1ContractedBlock.gifExpandedBlockStart.gifSimpleCustomTypeDialogEdit#region SimpleCustomTypeDialogEdit
 2ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 3InBlock.gif    /// SimpleCustomType 值编辑器 for Modal
 4ExpandedSubBlockEnd.gif    /// </summary>

 5InBlock.gif    public class SimpleCustomTypeDialogEditor : UITypeEditor
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            if (context != null && context.Instance != null)
10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
11InBlock.gif                return UITypeEditorEditStyle.Modal;
12ExpandedSubBlockEnd.gif            }

13InBlock.gif            return base.GetEditStyle(context);
14ExpandedSubBlockEnd.gif        }

15InBlock.gif
16InBlock.gif        public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            System.Windows.Forms.Design.IWindowsFormsEditorService editorService = null;
19InBlock.gif            if (context != null && context.Instance != null && provider != null)
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                editorService = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
22InBlock.gif                if (editorService != null)
23ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
24InBlock.gif                    CustomControl control = (CustomControl)context.Instance;
25InBlock.gif                    FrmSimpleCustomTypeDialogEditor dlg = new FrmSimpleCustomTypeDialogEditor(control.DialogProperty);
26InBlock.gif                    if (dlg.ShowDialog() == DialogResult.OK)
27ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
28InBlock.gif                        value = dlg.SimpleCustomTypeProperty;
29InBlock.gif                        return value;
30ExpandedSubBlockEnd.gif                    }

31ExpandedSubBlockEnd.gif                }

32ExpandedSubBlockEnd.gif            }

33InBlock.gif
34InBlock.gif            return value;
35ExpandedSubBlockEnd.gif        }

36ExpandedSubBlockEnd.gif    }

37ExpandedBlockEnd.gif    #endregion

3、再添加一个属性,并用EditorAttribute 指定相应的编辑器类

 1ContractedBlock.gifExpandedBlockStart.gif弹出属性值编辑对话框的属性#region 弹出属性值编辑对话框的属性
 2InBlock.gif        // 注意:这里设置了复杂属性的初始值 new SimpleCustomType(1, 2)
 3InBlock.gif        private SimpleCustomType dialogField = new SimpleCustomType(12);
 4InBlock.gif
 5InBlock.gif        [Category("弹出式属性值编辑对话框")]
 6InBlock.gif        [TypeConverter(typeof(SimpleCustomTypeConverter))]
 7InBlock.gif        [Editor(typeof(SimpleCustomTypeDialogEditor), typeof(UITypeEditor))]
 8InBlock.gif        public SimpleCustomType DialogProperty
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn dialogField; }
11ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ dialogField = value; }
12ExpandedSubBlockEnd.gif        }

13ExpandedBlockEnd.gif        #endregion

The end.

转载于:https://www.cnblogs.com/luqingfei/archive/2007/03/14/674904.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值