c# Winform PropertyGrid 实现下拉框 多选

摘要:
=null&&context.Instance!=null&&provider!=null)25{26IWindowsFormsEditorServiceservice=(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));27if(service!

c# Winform PropertyGrid 实现下拉框 多选第1张

c# Winform PropertyGrid 实现下拉框 多选第2张

c# Winform PropertyGrid 实现下拉框 多选第3张c# Winform PropertyGrid 实现下拉框 多选第4张
1 usingPropertyGridHelpers.Controls;
2 usingSystem;
3 usingSystem.Collections.Generic;
4 usingSystem.ComponentModel;
5 usingSystem.Drawing.Design;
6 usingSystem.Windows.Forms;
7 usingSystem.Windows.Forms.Design;
8 
9 namespacePropertyGridHelpers.UIEditors
10 {
11 
12     public classFlagEnumUIEditor : UITypeEditor
13 {
14         privateCheckedListBoxEx check;
15 
16         publicFlagEnumUIEditor()
17 {
18             check = newCheckedListBoxEx();
19             check.BorderStyle =BorderStyle.None;
20 }
21 
22         public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, objectvalue)
23 {
24             if (context != null && context.Instance != null && provider != null)
25 {
26                 IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
27                 if (service != null)
28 {
29                     List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
30                     for (int i = 0; i < 100; i++)
31 {
32                         list.Add(new KeyValuePair<string, string>(i.ToString(), Guid.NewGuid().ToString("N")));
33 }
34                     check.DataSource =list;
35 service.DropDownControl(check);
36                     returncheck.GetSelectItemValueText;
37 }
38 }
39             return null;
40 }
41 
42         public overrideUITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
43 {
44             returnUITypeEditorEditStyle.DropDown;
45 }
46 }
47 }
FlagEnumUIEditor
c# Winform PropertyGrid 实现下拉框 多选第5张c# Winform PropertyGrid 实现下拉框 多选第6张
1 usingSystem;
2 usingSystem.ComponentModel;
3 usingSystem.Windows.Forms;
4 usingSystem.Collections.Generic;
5 usingSystem.Linq;
6 
7 namespacePropertyGridHelpers.Controls
8 {
9 
10     public classCheckedListBoxEx : CheckedListBox
11 {
12         private Container components = null;
13         publicCheckedListBoxEx()
14 {
15 InitializeComponent();
16 }
17         protected override void Dispose(booldisposing)
18 {
19             if(disposing)
20 {
21                 if (components != null)
22 components.Dispose();
23 }
24             base.Dispose(disposing);
25 }
26 
27         #region Component Designer generated code
28 
29         private voidInitializeComponent()
30 {
31             this.CheckOnClick = true;
32 }
33         protected override voidOnItemCheck(ItemCheckEventArgs e)
34 {
35             base.OnItemCheck(e);
36 }
37         #endregion
38 
39         #region Add
40         public CheckItem Add(string code, stringvalue)
41 {
42             CheckItem item = newCheckItem(code, value);
43 Items.Add(item);
44             returnitem;
45 }
46         publicCheckItem Add(CheckItem item)
47 {
48 Items.Add(item);
49             returnitem;
50 }
51         #endregion
52         #region 获取选择值
53         public string GetSelectItemValueText { get { return string.Join(",", GetSelectItemAll.Select(n =>n.Value)); } }
54         public string GetSelectItemKeyText { get { return string.Join(",", GetSelectItemAll.Select(n =>n.Key)); } }
55         public List<KeyValuePair<string, string>>GetSelectItemAll
56 {
57             get
58 {
59                 List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
60                 for (int i = 0; i < Items.Count; i++)
61 {
62                     CheckItem item = Items[i] asCheckItem;
63                     if(GetItemChecked(i))
64 {
65                         list.Add(new KeyValuePair<string, string>(item.Code, item.Value));
66 }
67 }
68                 returnlist;
69 }
70 }
71         #endregion
72         private objectdata;
73         /// <summary>
74         ///绑定数据源
75         /// </summary>
76         /// <param name="data"></param>
77         public new objectDataSource
78 {
79             get
80 {
81                 returndata;
82 }
83             set
84 {
85                 data =value;
86                 if (data is IEnumerable<KeyValuePair<string, string>>)
87 {
88                     foreach (KeyValuePair<string, string> item in (data as IEnumerable<KeyValuePair<string, string>>))
89 {
90 Add(item.Key, item.Value);
91 }
92 }
93                 else if (data is IEnumerable<CheckItem>)
94 {
95                     foreach (CheckItem item in (data as IEnumerable<CheckItem>))
96 {
97 Add(item);
98 }
99 }
100 }
101 }
102 
103 }
104 }
CheckedListBoxEx
c# Winform PropertyGrid 实现下拉框 多选第7张c# Winform PropertyGrid 实现下拉框 多选第8张
1 namespacePropertyGridHelpers.Controls
2 {
3     /// <summary>
4     ///Represents an item in the checklistbox
5     /// </summary>
6     public classCheckItem
7 {
8         public stringValue;
9         public stringCode;
10         public CheckItem(string code, stringvalue)
11 {
12             this.Value =value;
13             this.Code =code;
14 }
15         public override stringToString()
16 {
17             returnValue;
18 }
19 }
20 
21 }
CheckItem
c# Winform PropertyGrid 实现下拉框 多选第9张c# Winform PropertyGrid 实现下拉框 多选第10张
1 usingPropertyGridHelpers.UIEditors;
2 usingSystem.ComponentModel;
3 usingSystem.Windows.Forms;
4 
5 namespaceWindowsFormsApplication1
6 {
7     public partial classForm1 : Form
8 {
9         publicForm1()
10 {
11 InitializeComponent();
12             propertyGrid1.SelectedObject = newPropertyList(); ;
13 }
14 }
15     classPropertyList
16 {
17         stringm_dir;
18         [EditorAttribute(typeof(FlagEnumUIEditor), typeof(System.Drawing.Design.UITypeEditor))]
19         [DisplayName("Direction")]
20         [Description("Direction property")]
21         public stringDir
22 {
23             get
24 {
25                 returnm_dir;
26 }
27             set
28 {
29                 m_dir =value;
30 }
31 }
32 }
33  
34 }
Form1

源码分享

链接:https://pan.baidu.com/s/1e8D-WoTYmA-D7vm88TTQrA
提取码:bdrd
复制这段内容后打开百度网盘手机App,操作更方便哦

免责声明:文章转载自《c# Winform PropertyGrid 实现下拉框 多选》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇云存储:阿里云 和 七牛 的比较ag-grid动态生成表头及绑定表数据下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java使用jsoup时绕过https证书验证

详细错误信息: SunCertPathBuilderException: unable to find valid certification path to requested target 问题原因:爬相关数据,因该网站有SSL加密,故无法爬取。 问题解决之核心代码: /** * 绕过HTTPS验证 */ static publi...

FTP操作(FTPClient)

//FTP开源封装的类 using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP {     /// <summary>     /// FTP客户端操作类     /// </summary>  ...

Guava LoadingCache不能缓存null值

测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了。后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候会抛出异常。 通过几个例子开看这个问题: public void test_loadNull() { LoadingCache<String,...

实践Kong for Kubernetes(K8S),kong最新2.1版本和kong-ingress-controller:0.9.1版本

先决条件 Kubernetes集群:您可以使用Minikube或GKE集群。Kong与Kubernetes的所有发行版兼容。 kubectl访问权限:您应该已经kubectl安装并配置为与Kubernetes集群通信。 为Kubernetes安装Kong 使用以下安装方法之一安装Kong for Kubernetes: YAML清单 helm K...

jsp页面渲染

1.jsp    1.jsp脚本和注释     1)<%java代码%> --------------内部的java代码翻译到service方法的内部     2)<%=java变量或表达式%>  ----------会被翻译成service 方法内部 out.print()     3)<%! java代码%> ---...

C#连接MySQL数据库

本文章是建立在已经安装MySQL数据库的前提,默认安装在C:Program Files (x86)MySQL,建议在安装时选中Connector.NET 6.9的安装,里面有MySQL与C#连接的动态链接库。 帮助文档C:Program Files (x86)MySQLConnector.NET 6.9DocumentationConnectorNET.c...