c#多选下拉框(ComboBox)

代码如下

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Collections;
 10 using System.Reflection;
 11 using Newtonsoft.Json.Linq;
 12
 13 namespace ControlsAA
 14 {
 15     public class ComboBoxEx : ComboBox
 16     {
 17         TreeView lst = new TreeView();
 18
 19         public ComboBoxEx()
 20         {
 21             this.DrawMode = DrawMode.OwnerDrawFixed;//只有设置这个属性为OwnerDrawFixed才可能让重画起作用
 22             lst.KeyUp += new KeyEventHandler(lst_KeyUp);
 23             lst.MouseUp += new MouseEventHandler(lst_MouseUp);
 24             // lst.KeyDown += new KeyEventHandler(lst_KeyDown);
 25             lst.Leave += new EventHandler(lst_Leave);
 26             lst.CheckBoxes = true;
 27             lst.ShowLines = false;
 28             lst.ShowPlusMinus = false;
 29             lst.ShowRootLines = false;
 30             this.DropDownHeight = 1;
 31         }
 32
 33         void lst_Leave(object sender, EventArgs e)
 34         {
 35             lst.Hide();
 36         }
 37         #region Property
 38
 39         [Description("选定项的值"), Category("Data")]
 40         public List<TreeNode> SelectedItems
 41         {
 42             get
 43             {
 44                 List<TreeNode> lsttn = new List<TreeNode>();
 45                 foreach (TreeNode tn in lst.Nodes)
 46                 {
 47                     if (tn.Checked)
 48                     {
 49                         lsttn.Add(tn);
 50                     }
 51                 }
 52                 return lsttn;
 53             }
 54         }
 55
 56         /// <summary>
 57         /// 数据源
 58         /// </summary>
 59         [Description("数据源"), Category("Data")]
 60         public object DataSource
 61         {
 62             get;
 63             set;
 64         }
 65         /// <summary>
 66         /// 显示字段
 67         /// </summary>
 68         [Description("显示字段"), Category("Data")]
 69         public string DisplayFiled
 70         {
 71             get;
 72             set;
 73         }
 74         /// <summary>
 75         /// 值字段
 76         /// </summary>
 77         [Description("值字段"), Category("Data")]
 78         public string ValueFiled
 79         {
 80             get;
 81             set;
 82         }
 83         #endregion
 84
 85
 86         public void DataBind()
 87         {
 88             this.BeginUpdate();
 89             if (DataSource != null)
 90             {
 91                 if (DataSource is IDataReader)
 92                 {
 93                     DataTable dataTable = new DataTable();
 94                     dataTable.Load(DataSource as IDataReader);
 95
 96                     DataBindToDataTable(dataTable);
 97                 }
 98                 else if (DataSource is DataView || DataSource is DataSet || DataSource is DataTable)
 99                 {
100                     DataTable dataTable = null;
101
102                     if (DataSource is DataView)
103                     {
104                         dataTable = ((DataView)DataSource).ToTable();
105                     }
106                     else if (DataSource is DataSet)
107                     {
108                         dataTable = ((DataSet)DataSource).Tables[0];
109                     }
110                     else
111                     {
112                         dataTable = ((DataTable)DataSource);
113                     }
114
115                     DataBindToDataTable(dataTable);
116                 }
117                 else if (DataSource is IEnumerable)
118                 {
119                     DataBindToEnumerable((IEnumerable)DataSource);
120                 }
121                 else
122                 {
123                     throw new Exception("DataSource doesn‘t support data type: " + DataSource.GetType().ToString());
124                 }
125             }
126             else
127             {
128                 lst.Nodes.Clear();
129             }
130
131             lst.ItemHeight = this.ItemHeight;
132             lst.BorderStyle = BorderStyle.FixedSingle;
133             lst.Size = new Size(this.DropDownWidth, this.ItemHeight * (this.MaxDropDownItems - 1) - (int)this.ItemHeight / 2);
134             lst.Location = new Point(this.Left, this.Top + this.ItemHeight + 6);
135             this.Parent.Controls.Add(lst);
136             lst.Hide();
137             this.EndUpdate();
138         }
139
140
141         private void DataBindToDataTable(DataTable dt)
142         {
143             foreach (DataRow dr in dt.Rows)
144             {
145                 TreeNode tn = new TreeNode();
146                 if (!string.IsNullOrEmpty(DisplayFiled) && !string.IsNullOrEmpty(ValueFiled))
147                 {
148                     tn.Text = dr[DisplayFiled].ToString();
149                     tn.Tag = dr[ValueFiled].ToString();
150                 }
151                 else if (string.IsNullOrEmpty(ValueFiled))
152                 {
153                     tn.Text = dr[DisplayFiled].ToString();
154                     tn.Tag = dr[DisplayFiled].ToString();
155                 }
156                 else if (string.IsNullOrEmpty(DisplayFiled))
157                 {
158                     tn.Text = dr[ValueFiled].ToString();
159                     tn.Tag = dr[ValueFiled].ToString();
160                 }
161                 else
162                 {
163                     throw new Exception("ValueFiled和DisplayFiled至少保证有一项有值");
164                 }
165
166                 tn.Checked = false;
167                 lst.Nodes.Add(tn);
168             }
169         }
170
171         /// <summary>
172         /// 绑定到可枚举类型
173         /// </summary>
174         /// <param name="enumerable">可枚举类型</param>
175         private void DataBindToEnumerable(IEnumerable enumerable)
176         {
177             IEnumerator enumerator = enumerable.GetEnumerator();
178             while (enumerator.MoveNext())
179             {
180                 object currentObject = enumerator.Current;
181                 lst.Nodes.Add(CreateListItem(currentObject));
182             }
183         }
184
185
186
187         private TreeNode CreateListItem(Object obj)
188         {
189             TreeNode item = new TreeNode();
190
191             if (obj is string)
192             {
193                 item.Text = obj.ToString();
194                 item.Tag = obj.ToString();
195             }
196             else
197             {
198                 if (DisplayFiled != "")
199                 {
200                     item.Text = GetPropertyValue(obj, DisplayFiled);
201                 }
202                 else
203                 {
204                     item.Text = obj.ToString();
205                 }
206
207                 if (ValueFiled != "")
208                 {
209                     item.Tag = GetPropertyValue(obj, ValueFiled);
210                 }
211                 else
212                 {
213                     item.Tag = obj.ToString();
214                 }
215             }
216             return item;
217         }
218
219
220         private string GetPropertyValue(object obj, string propertyName)
221         {
222             object result = null;
223
224             result = ObjectUtil.GetPropertyValue(obj, propertyName);
225             return result == null ? String.Empty : result.ToString();
226         }
227
228         #region override
229
230
231         protected override void OnKeyUp(KeyEventArgs e)
232         {
233             base.OnKeyDown(e);
234             bool Pressed = (e.Control && ((e.KeyData & Keys.A) == Keys.A));
235             if (Pressed)
236             {
237                 this.Text = "";
238                 for (int i = 0; i < lst.Nodes.Count; i++)
239                 {
240                     lst.Nodes[i].Checked = true;
241                     if (this.Text != "")
242                     {
243                         this.Text += ",";
244                     }
245                     this.Text += lst.Nodes[i].Tag;
246                 }
247             }
248         }
249
250         protected override void OnMouseDown(MouseEventArgs e)
251         {
252             this.DroppedDown = false;
253
254         }
255
256         protected override void OnMouseUp(MouseEventArgs e)
257         {
258             this.DroppedDown = false;
259             lst.Focus();
260         }
261
262         protected override void OnDropDown(EventArgs e)
263         {
264             string strValue = this.Text;
265             if (!string.IsNullOrEmpty(strValue))
266             {
267                 List<string> lstvalues = strValue.Split(‘,‘).ToList<string>();
268                 foreach (TreeNode tn in lst.Nodes)
269                 {
270                     if (tn.Checked && !lstvalues.Contains(tn.Tag.ToString()) && !string.IsNullOrEmpty(tn.Tag.ToString().Trim()))
271                     {
272                         tn.Checked = false;
273                     }
274                     else if (!tn.Checked && lstvalues.Contains(tn.Tag.ToString()) && !string.IsNullOrEmpty(tn.Tag.ToString().Trim()))
275                     {
276                         tn.Checked = true;
277                     }
278                 }
279             }
280
281             lst.Show();
282
283         }
284         #endregion
285
286         private void lst_KeyUp(object sender, KeyEventArgs e)
287         {
288             this.OnKeyUp(e);
289         }
290
291         private void lst_MouseUp(object sender, MouseEventArgs e)
292         {
293             try
294             {
295                 this.Text = "";
296                 for (int i = 0; i < lst.Nodes.Count; i++)
297                 {
298                     if (lst.Nodes[i].Checked)
299                     {
300                         if (this.Text != "")
301                         {
302                             this.Text += ",";
303                         }
304                         this.Text += lst.Nodes[i].Tag;
305                     }
306                 }
307             }
308             catch
309             {
310                 this.Text = "";
311             }
312             bool isControlPressed = (Control.ModifierKeys == Keys.Control);
313             bool isShiftPressed = (Control.ModifierKeys == Keys.Shift);
314             if (isControlPressed || isShiftPressed)
315                 lst.Show();
316             else
317                 lst.Hide();
318         }
319
320     }
321
322
323     /// <summary>
324     /// 对象帮助类
325     /// </summary>
326     public class ObjectUtil
327     {
328         /// <summary>
329         /// 获取对象的属性值
330         /// </summary>
331         /// <param name="obj">可能是DataRowView或一个对象</param>
332         /// <param name="propertyName">属性名</param>
333         /// <returns>属性值</returns>
334         public static object GetPropertyValue(object obj, string propertyName)
335         {
336             object result = null;
337
338             try
339             {
340                 if (obj is DataRow)
341                 {
342                     result = (obj as DataRow)[propertyName];
343                 }
344                 else if (obj is DataRowView)
345                 {
346                     result = (obj as DataRowView)[propertyName];
347                 }
348                 else if (obj is JObject)
349                 {
350                     result = (obj as JObject).Value<JValue>(propertyName).Value; //.getValue(propertyName);
351                 }
352                 else
353                 {
354                     result = GetPropertyValueFormObject(obj, propertyName);
355                 }
356             }
357             catch (Exception)
358             {
359                 // 找不到此属性
360             }
361
362             return result;
363         }
364
365         /// <summary>
366         /// 获取对象的属性值
367         /// </summary>
368         /// <param name="obj">对象</param>
369         /// <param name="propertyName">属性名("Color"、"BodyStyle"或者"Info.UserName")</param>
370         /// <returns>属性值</returns>
371         private static object GetPropertyValueFormObject(object obj, string propertyName)
372         {
373             object rowObj = obj;
374             object result = null;
375
376             if (propertyName.IndexOf(".") > 0)
377             {
378                 string[] properties = propertyName.Split(‘.‘);
379                 object tmpObj = rowObj;
380
381                 for (int i = 0; i < properties.Length; i++)
382                 {
383                     PropertyInfo property = tmpObj.GetType().GetProperty(properties[i], BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
384                     if (property != null)
385                     {
386                         tmpObj = property.GetValue(tmpObj, null);
387                     }
388                 }
389
390                 result = tmpObj;
391             }
392             else
393             {
394                 PropertyInfo property = rowObj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
395                 if (property != null)
396                 {
397                     result = property.GetValue(rowObj, null);
398                 }
399             }
400
401             return result;
402         }
403     }
404 }

需要引用的文件在这里http://pan.baidu.com/s/1qXa97UO

时间: 2024-10-10 23:53:20

c#多选下拉框(ComboBox)的相关文章

Html 多选下拉框

1.css文件 span.dropList {display:inline-block; height:20px;border:1px solid #ccc;  cursor:pointer; background:url(../images/xj.png) #fff no-repeat center right; font-size:12px; line-height:20px;padding-right:10px; position:relative;} span.dropList .Che

Extjs4.2 多选下拉框

//多选下拉框 Ext.define('MDM.view.custom.MultiComboBox', { extend: 'Ext.form.ComboBox', alias: 'widget.multicombobox', xtype: 'multicombobox', initComponent: function() { this.multiSelect = true; this.listConfig = { itemTpl: Ext.create('Ext.XTemplate', '<

ExtJs5.1多选下拉框CheckComb

ExtJs这么多个版本了,可就是不提供多选下拉框,老外不用这个玩意吗? 5都出来这么久了,新写的项目就用5吧,把以前Extjs4.2的时搜到前人的CheckComb改巴改巴,能用了就赶紧贴上来,没有仔细测试各种情况. Ext.define('Ext.ux.CheckCombo', { extend : 'Ext.form.field.ComboBox', alias : 'widget.checkcombo', multiSelect : true, allSelector : false, n

c# 复选下拉框

引用dll: http://pan.baidu.com/s/1qXa97UO 自定义类: namespace TMI_S { /// <summary> /// 功能描述:自定义多选下拉框 /// 作 者:huangzh /// 创建日期:2016-01-04 11:57:13 /// 任务编号: /// </summary> public class MultiComboBox : ComboBox { public TreeView lst = new TreeView();

query多选下拉框插件 jquery-multiselect(修改)

其实网上关于该控件的使用教程已经很多了,其中 query多选下拉框插件 jquery-multiselect Jquery多选下拉列表插件jquery multiselect功能介绍及使用 这2个的介绍已经比较详细了,尤其是第二个有扩展MyValues函数,只是扩展有些bug,这里我在提出一些我的扩展,我们应该把multiValues属性定义在options里面,让每个multiselect控件都有自己的multiValues属性.我这里还需要一个获取text的方法.有关Myvalues和Myt

多选下拉框带搜索(aps.net)

自己写了一个带搜索功能的多选下拉框,为了要获取值,就没有封装插件,实现思路 1.一个文本框 做搜索 2.一个文本框显示选中文本,一个隐藏控件存值 3.一个div里面绑定CheckBoxList控件(这里,你也可以请求后直接插入checkbox) 代码 1.一个变量把div的html存起来 2.选中就加到文本框,取消就减掉 //选中记录,反选删除 $("#divChkList :checkbox").each(function () { $(this).click(function ()

Bootstrap下拉框 Combobox

Bootstrap下拉框 Combobox显示效果如下: 源代码: <select class="combobox"> <option></option> <option value="PA">Pennsylvania</option> <option value="CT">Connecticut</option> <option value="N

页面多选下拉框 jquery.multiple.select

1.首先引用jquery.multiple.select.js.multiple-select.css 2.在页面select下拉列表里面加一个multiple="multiple" <select name="nimbusHost" id="nimbusHost" multiple="multiple"> <c:forEach items="${stormIps}" var="

MVC实现多选下拉框

借助Chosen Plugin可以实现多选下拉框. 选择多项: 设置选项数量,比如设置最多允许2个选项: 考虑到多选下拉<select multiple="multiple"...></select>选中项是string数组,Model应该这样设计: using System.Collections.Generic; using System.Web.Mvc; namespace MvcApplication1.Models { public class Car

自定义SWT控件二之自定义多选下拉框

2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CLabel; import or