自定义 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或别的形式出现,如
 1         public  Size _Size
 2          {
 3
 4            get {
 5                    return size;
 6            }

 7            set {
 8               
 9                size = value;
10            }

11        }

这时,和这样的一个属性交互,就会涉及到 类型转换( TypeConverter)了

那么,今天就来和大家一起试图解决以上2问题:属性与Property Editor 的交互, TypeConverter

最终效果图:
以窗体的形式显示
(图A)

以下拉的形式出现:(图B)




 1 internal   class  ItemEditor : UITypeEditor
 2      {
 3
 4        Methods..#region Methods..
 5        /**//// <summary>
 6        /// 初始化 <see cref="ButtonsEditor"/> 的实例
 7        /// </summary>

 8        public ItemEditor() { }
 9
10        /**//// <summary>
11        /// 获取由 <see cref="EditValue"/> 方法使用的编辑器样式
12        /// </summary>
13        /// <param name="context">可用于获取附加上下文信息的 <see cref="ITypeDescriptorContext"/></param>
14        /// <returns><see cref="UITypeEditorEditStyle"/> 值,指示 <see cref="EditValue"/> 使用的编辑器的样式。如果 <see cref="UITypeEditor"/> 不支持该方法,则 GetEditStyle 将返回 <see cref="UITypeEditorEditStyle.None"/></returns>

15        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
16        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
17        {
18            return UITypeEditorEditStyle.Modal;
19        }

20
21        /**//// <summary>
22        /// 使用由 <see cref="GetEditStyle"/> 指示的编辑器样式编辑指定对象的值。
23        /// </summary>
24        /// <param name="context">可用于获取附加上下文信息的 <see cref="ITypeDescriptorContext"/>。 </param>
25        /// <param name="provider"><see cref="IServiceProvider"/>,此编辑器可用其来获取服务。</param>
26        /// <param name="value">要编辑的对象。</param>
27        /// <returns>新的对象值。</returns>

28        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
29        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
30        {
31            
32            
33            IWindowsFormsEditorService service1 = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
34            if (service1 == null)
35            {
36                return value;
37            }

38            ListItemCollection LI=null;
39
40            
41            if (context.Instance is ListBox)
42            {
43               
44                LI = ((ListBox)context.Instance).ListItemCollection;
45            }

46            else if (context.Instance is object[])
47            {
48               
49                object[] objArray1 = (object[])context.Instance;
50                if (objArray1[0is ListItemCollection)
51                {
52                    LI = (ListItemCollection)objArray1[0];
53                }

54            }

55            
56
57
58            ListItemCollection templistItems = new ListItemCollection();
59            if (LI != null)
60            {
61                IEnumerator myEnumerator = LI.GetEnumerator();
62
63                while ((myEnumerator.MoveNext()) && (myEnumerator.Current != null))
64                {
65                    templistItems.Add((ListItem)myEnumerator.Current);
66                }

67            }

68
69            ItemEditorForm bef = new ItemEditorForm(templistItems);
70            if (service1.ShowDialog(bef) == DialogResult.OK)
71            {
72                value = templistItems;
73            }

74            else
75            {
76                
77                if (templistItems == null || templistItems.Count <= 0)
78                    value = null;
79                else
80                    value = LI;
81
82            }

83            return value;
84            
85        }

86        #endregion

87
88    }




 1 class  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    }

自定义一个UserControl,作为下拉列表出现
(图C)  ItemEditorControl.cs


 1 public   partial   class  ItemEditorControl : UserControl
 2      {
 3
 4        private Size size;
 5
 6
 7
 8        属性#region 属性
 9        public Size _Size
10        {
11            get {
12
13                if (size != null)
14                    return size;
15                return new Size(); ;
16            }

17            set {
18
19                size = value;
20                
21            }

22        }

23        #endregion

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    }


下载源代码

转载于:https://www.cnblogs.com/weijunbao/archive/2008/01/24/1051264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值