c# Winform PropertyGrid 实现下拉框 多选

 1 using PropertyGridHelpers.Controls;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Drawing.Design;
 6 using System.Windows.Forms;
 7 using System.Windows.Forms.Design;
 8
 9 namespace PropertyGridHelpers.UIEditors
10 {
11
12     public class FlagEnumUIEditor : UITypeEditor
13     {
14         private CheckedListBoxEx check;
15
16         public FlagEnumUIEditor()
17         {
18             check = new CheckedListBoxEx();
19             check.BorderStyle = BorderStyle.None;
20         }
21
22         public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
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                     return check.GetSelectItemValueText;
37                 }
38             }
39             return null;
40         }
41
42         public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
43         {
44             return UITypeEditorEditStyle.DropDown;
45         }
46     }
47 }

FlagEnumUIEditor

  1 using System;
  2 using System.ComponentModel;
  3 using System.Windows.Forms;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6
  7 namespace PropertyGridHelpers.Controls
  8 {
  9
 10     public class CheckedListBoxEx : CheckedListBox
 11     {
 12         private Container components = null;
 13         public CheckedListBoxEx()
 14         {
 15             InitializeComponent();
 16         }
 17         protected override void Dispose(bool disposing)
 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 void InitializeComponent()
 30         {
 31             this.CheckOnClick = true;
 32         }
 33         protected override void OnItemCheck(ItemCheckEventArgs e)
 34         {
 35             base.OnItemCheck(e);
 36         }
 37         #endregion
 38
 39         #region Add
 40         public CheckItem Add(string code, string value)
 41         {
 42             CheckItem item = new CheckItem(code, value);
 43             Items.Add(item);
 44             return item;
 45         }
 46         public CheckItem Add(CheckItem item)
 47         {
 48             Items.Add(item);
 49             return item;
 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] as CheckItem;
 63                     if (GetItemChecked(i))
 64                     {
 65                         list.Add(new KeyValuePair<string, string>(item.Code, item.Value));
 66                     }
 67                 }
 68                 return list;
 69             }
 70         }
 71         #endregion
 72         private object data;
 73         /// <summary>
 74         /// 绑定数据源
 75         /// </summary>
 76         /// <param name="data"></param>
 77         public new object DataSource
 78         {
 79             get
 80             {
 81                 return data;
 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

 1 namespace PropertyGridHelpers.Controls
 2 {
 3     /// <summary>
 4     /// Represents an item in the checklistbox
 5     /// </summary>
 6     public class CheckItem
 7     {
 8         public string Value;
 9         public string Code;
10         public CheckItem(string code, string value)
11         {
12             this.Value = value;
13             this.Code = code;
14         }
15         public override string ToString()
16         {
17             return Value;
18         }
19     }
20
21 }

CheckItem

 1 using PropertyGridHelpers.UIEditors;
 2 using System.ComponentModel;
 3 using System.Windows.Forms;
 4
 5 namespace WindowsFormsApplication1
 6 {
 7     public partial class Form1 : Form
 8     {
 9         public Form1()
10         {
11             InitializeComponent();
12             propertyGrid1.SelectedObject = new PropertyList(); ;
13         }
14     }
15     class PropertyList
16     {
17         string m_dir;
18         [EditorAttribute(typeof(FlagEnumUIEditor), typeof(System.Drawing.Design.UITypeEditor))]
19         [DisplayName("Direction")]
20         [Description("Direction property")]
21         public string Dir
22         {
23             get
24             {
25                 return m_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,操作更方便哦

原文地址:https://www.cnblogs.com/aaaaq/p/10802083.html

时间: 2024-10-18 07:09:09

c# Winform PropertyGrid 实现下拉框 多选的相关文章

jquery实现下拉框多选

一.说明 本文是利用EasyUI实现下拉框多选功能,在ComboxTree其原有的基础上对样式进行了改进,样式表已上传demo,代码如下 二.代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w

ops-web运维平台-create.jsp-mootools下拉框-复选框

create.jsp页面的,body部分 <body onload="Page.init('${pageError}','${pageMessage}',${isSubmit},true)"> <div id="title">${pageTitle}</div> <s:form id="MYFORM" action="%{pageAction}" method="post&q

bootstrap-select实现下拉框多选效果

bootstrap-select实现下拉框多选效果 听语音 在学习bootstrap实现下拉多选效果的时候,觉得该效果很好,所以拿来分享下,这里就不详细的描述了,直接附上代码给各位看看 方法/步骤 1 最终实现的效果: 2 HTML代码: <div class= "row" style ="margin-top :10px;"> <div class= "form-group col-xs-12"> <label f

winform dataGridView DataGridViewComboBoxColumn 下拉框事件

有一个dataGridView ,有一列是DataGridViewComboBoxColumn .用动态绑定,在绑定数据的时候.我们也给这一列绑定数据 在dataGridView的RowsAdded事件中写代码 /// <summary> /// 添加新行 /// </summary> /// <param name="sender"></param> /// <param name="e"></pa

Winform实现ComboBox下拉框与数据库的绑定

实现效果如下: 1.设计窗体 下拉框的名称cmbName 2.连接数据库 DBHelper类代码: class DBHelper { /// <summary> /// 创建静态连接字符串 /// </summary> private static string connString= "Data Source=.;Initial Catalog=Test;uid=sa;pwd=F123456789f"; public static SqlConnection

angularJs实现下拉框多选

话不多说,直接上干货. 肯定需要下拉选插件.必须引入的是   注意 先后顺序 select2.css select2-bootstrap.css select2.min.js angular.min.js angular-select2.js ok,然后只需要写上一段代码就ok,如下 <input select2 select2-model="entity.brandIds" config="brandList" multiple placeholder=&q

帆软下拉框不选为空可选择显示全部值

方法一:sql查询不传参数  模板→模板参数→添加参数默认值 参数设置 https://www.cnblogs.com/zhuyu139/p/12066574.html 单元格过滤公式→if(len($大区)==0,nofilter,$大区) 预览即可 方法二:sql查询语句 ${if(len(area) == 0,"","and 货主地区 = '" + area + "'")} sql做了筛选操作,下拉重新设置为数据查询 原文地址:https:

MVC5 下拉框(多选)

1.Model [Display(Name = "职位")] [Required] public int[] job { get; set; } //职位属性 public IEnumerable<Item> joblist { get; set; } //多选框属性 2.cotroller [Description("职位绑定")] [LoginAllowView] private List<Item> bindPosts() { Stri

jQuery select下拉框设置选中项

$("#selectId option:last").prop("selected", 'selected'); $("#selectId option").eq(0).attr("selected", true);