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();
Form_Main form_Call;
public MultiComboBox(Form_Main form_Call)
{
this.form_Call = form_Call;
this.DrawMode = DrawMode.OwnerDrawFixed;//只有设置这个属性为OwnerDrawFixed才可能让重画起作用
lst.KeyUp += new KeyEventHandler(lst_KeyUp);
lst.MouseUp += new MouseEventHandler(lst_MouseUp);
// lst.KeyDown += new KeyEventHandler(lst_KeyDown);
lst.Leave += new EventHandler(lst_Leave);
lst.CheckBoxes = true;
lst.ShowLines = false;
lst.ShowPlusMinus = false;
lst.ShowRootLines = false;
this.DropDownHeight = ;
} void lst_Leave(object sender, EventArgs e)
{
lst.Hide();
}
#region Property [Description("选定项的值"), Category("Data")]
public List<TreeNode> SelectedItems
{
get
{
List<TreeNode> lsttn = new List<TreeNode>();
foreach (TreeNode tn in lst.Nodes)
{
if (tn.Checked)
{
lsttn.Add(tn);
}
}
return lsttn;
}
} /// <summary>
/// 数据源
/// </summary>
[Description("数据源"), Category("Data")]
public object DataSource
{
get;
set;
}
/// <summary>
/// 显示字段
/// </summary>
[Description("显示字段"), Category("Data")]
public string DisplayFiled
{
get;
set;
}
/// <summary>
/// 值字段
/// </summary>
[Description("值字段"), Category("Data")]
public string ValueFiled
{
get;
set;
}
#endregion public void DataBind()
{
this.BeginUpdate();
if (DataSource != null)
{
if (DataSource is IDataReader)
{
DataTable dataTable = new DataTable();
dataTable.Load(DataSource as IDataReader); DataBindToDataTable(dataTable);
}
else if (DataSource is DataView || DataSource is DataSet || DataSource is DataTable)
{
DataTable dataTable = null; if (DataSource is DataView)
{
dataTable = ((DataView)DataSource).ToTable();
}
else if (DataSource is DataSet)
{
dataTable = ((DataSet)DataSource).Tables[];
}
else
{
dataTable = ((DataTable)DataSource);
} DataBindToDataTable(dataTable);
}
else if (DataSource is IEnumerable)
{
DataBindToEnumerable((IEnumerable)DataSource);
}
else
{
throw new Exception("DataSource doesn't support data type: " + DataSource.GetType().ToString());
}
}
else
{
lst.Nodes.Clear();
} lst.ItemHeight = this.ItemHeight;
lst.BorderStyle = BorderStyle.FixedSingle;
lst.Size = new Size(this.DropDownWidth, this.ItemHeight * (this.MaxDropDownItems - ) - (int)this.ItemHeight / );
lst.Location = new Point(this.Left, this.Top + this.ItemHeight + );
this.Controls.Add(lst);
lst.Hide();
this.EndUpdate();
} private void DataBindToDataTable(DataTable dt)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
if (!string.IsNullOrEmpty(DisplayFiled) && !string.IsNullOrEmpty(ValueFiled))
{
tn.Text = dr[DisplayFiled].ToString();
tn.Tag = dr[ValueFiled].ToString();
}
else if (string.IsNullOrEmpty(ValueFiled))
{
tn.Text = dr[DisplayFiled].ToString();
tn.Tag = dr[DisplayFiled].ToString();
}
else if (string.IsNullOrEmpty(DisplayFiled))
{
tn.Text = dr[ValueFiled].ToString();
tn.Tag = dr[ValueFiled].ToString();
}
else
{
throw new Exception("ValueFiled和DisplayFiled至少保证有一项有值");
} tn.Checked = false;
lst.Nodes.Add(tn);
}
} /// <summary>
/// 绑定到可枚举类型
/// </summary>
/// <param name="enumerable">可枚举类型</param>
private void DataBindToEnumerable(IEnumerable enumerable)
{
IEnumerator enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
{
object currentObject = enumerator.Current;
lst.Nodes.Add(CreateListItem(currentObject));
}
} private TreeNode CreateListItem(Object obj)
{
TreeNode item = new TreeNode(); if (obj is string)
{
item.Text = obj.ToString();
item.Tag = obj.ToString();
}
else
{
if (DisplayFiled != "")
{
item.Text = GetPropertyValue(obj, DisplayFiled);
}
else
{
item.Text = obj.ToString();
} if (ValueFiled != "")
{
item.Tag = GetPropertyValue(obj, ValueFiled);
}
else
{
item.Tag = obj.ToString();
}
}
return item;
} private string GetPropertyValue(object obj, string propertyName)
{
object result = null; result = ObjectUtil.GetPropertyValue(obj, propertyName);
return result == null ? String.Empty : result.ToString();
} #region override protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyDown(e);
bool Pressed = (e.Control && ((e.KeyData & Keys.A) == Keys.A));
if (Pressed)
{
this.Text = "";
for (int i = ; i < lst.Nodes.Count; i++)
{
lst.Nodes[i].Checked = true;
if (this.Text != "")
{
this.Text += ",";
}
this.Text += lst.Nodes[i].Tag;
}
}
} protected override void OnMouseDown(MouseEventArgs e)
{
this.DroppedDown = false; } protected override void OnMouseUp(MouseEventArgs e)
{
this.DroppedDown = false;
lst.Focus();
} protected override void OnDropDown(EventArgs e)
{
if (lst != null) { lst.Nodes.Clear(); }
DataSet itemSet = new DataSet();
DataTable dt = new DataTable();
List<ImagePropertyModule> IPMS = ImagePropertyServices.GetImagePropertyInfo("localhost");
dt.Columns.Add("PropertyValue", Type.GetType("System.String"));
foreach (ImagePropertyModule ipm in IPMS)
{
DataRow dr = dt.NewRow();
dr["PropertyValue"] = ipm.PropertyValue;
dt.Rows.Add(dr);
}
itemSet.Tables.Add(dt);
this.DataSource = itemSet;
DataBind();
lst.Location=new Point(, );
form_Call.Controls.Add(lst);
lst.BringToFront();
string strValue = this.Text;
if (!string.IsNullOrEmpty(strValue))
{
List<string> lstvalues = strValue.Split(',').ToList<string>();
foreach (TreeNode tn in lst.Nodes)
{
if (tn.Checked && !lstvalues.Contains(tn.Tag.ToString()) && !string.IsNullOrEmpty(tn.Tag.ToString().Trim()))
{
tn.Checked = false;
}
else if (!tn.Checked && lstvalues.Contains(tn.Tag.ToString()) && !string.IsNullOrEmpty(tn.Tag.ToString().Trim()))
{
tn.Checked = true;
}
}
} lst.Show(); }
#endregion private void lst_KeyUp(object sender, KeyEventArgs e)
{
this.OnKeyUp(e);
} private void lst_MouseUp(object sender, MouseEventArgs e)
{
try
{
this.Text = "";
for (int i = ; i < lst.Nodes.Count; i++)
{
if (lst.Nodes[i].Checked)
{
if (this.Text != "")
{
this.Text += ",";
}
this.Text += lst.Nodes[i].Tag;
}
}
}
catch
{
this.Text = "";
}
bool isControlPressed = (Control.ModifierKeys == Keys.Control);
bool isShiftPressed = (Control.ModifierKeys == Keys.Shift);
if (isControlPressed || isShiftPressed)
lst.Show();
else
lst.Hide();
}
} /// <summary>
/// 对象帮助类
/// </summary>
public class ObjectUtil
{
/// <summary>
/// 获取对象的属性值
/// </summary>
/// <param name="obj">可能是DataRowView或一个对象</param>
/// <param name="propertyName">属性名</param>
/// <returns>属性值</returns>
public static object GetPropertyValue(object obj, string propertyName)
{
object result = null; try
{
if (obj is DataRow)
{
result = (obj as DataRow)[propertyName];
}
else if (obj is DataRowView)
{
result = (obj as DataRowView)[propertyName];
}
else if (obj is JObject)
{
result = (obj as JObject).Value<JValue>(propertyName).Value; //.getValue(propertyName);
}
else
{
result = GetPropertyValueFormObject(obj, propertyName);
}
}
catch (Exception)
{
// 找不到此属性
} return result;
} /// <summary>
/// 获取对象的属性值
/// </summary>
/// <param name="obj">对象</param>
/// <param name="propertyName">属性名("Color"、"BodyStyle"或者"Info.UserName")</param>
/// <returns>属性值</returns>
private static object GetPropertyValueFormObject(object obj, string propertyName)
{
object rowObj = obj;
object result = null; if (propertyName.IndexOf(".") > )
{
string[] properties = propertyName.Split('.');
object tmpObj = rowObj; for (int i = ; i < properties.Length; i++)
{
PropertyInfo property = tmpObj.GetType().GetProperty(properties[i], BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (property != null)
{
tmpObj = property.GetValue(tmpObj, null);
}
} result = tmpObj;
}
else
{
PropertyInfo property = rowObj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (property != null)
{
result = property.GetValue(rowObj, null);
}
} return result;
}
}
}
引用code:
MultiComboBox comboBoxEx = new MultiComboBox(this);
comboBoxEx.DisplayFiled = "PropertyValue";
comboBoxEx.DataBind();
comboBoxEx.Location = new Point(,);
comboBoxEx.Width = ;
comboBoxEx.Height = ;
this.groupBox11.Controls.Add(comboBoxEx);
c# 复选下拉框的更多相关文章
- pentaho cde 自定义复选下拉框 checkbox select
pentaho 自带的component 虽多,但是当用户需要在一个表格中查看多个组别的数据时,pentaho自带的单选框就不能实现了,所以复选下拉框势在必行,实现效果如下: 实现原理是借用了jqu ...
- bootstrap-multiselect.js多选下拉框初始化时默认选中初始值
bootstrap-multiselect.js多选下拉框默认值设置 一.案例数据格式 二.HTML代码 <select id="msgRoles" multiple=&q ...
- Easyui-Combobox多选下拉框
因为工作需要,引入combobox多选下拉框,并且获取选择的值并以","分开. 效果如下: 代码如下: <html> <head> <title> ...
- Extjs4.2 多选下拉框
//多选下拉框 Ext.define('MDM.view.custom.MultiComboBox', { extend: 'Ext.form.ComboBox', alias: 'widget.mu ...
- js:jquery multiSelect 多选下拉框实例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DropDownList单选与多选下拉框
一.单选DropDownList传值 1.添加界面的DropDownList显示值问题 (1)在方法内添加ViewData的方法: var ad = new UnitsRepository(); Vi ...
- Bootstrap3级联多选下拉框
<!DOCTYPE html> <html> <head> <title>Bootstrap3级联多选下拉框</title> <met ...
- js怎么能取得多选下拉框选中的多个值?
方法:获取多选下拉框对象数组→循环判断option选项的selected属性(true为选中,false为未选中)→使用value属性取出选中项的值.实例演示如下: 1.HTML结构 1 2 3 4 ...
- js多选下拉框
1.js原生实现 1.1:引用JS文件 /*! jQuery v1.12.4 | (c) jQuery Foundation | jquery.org/license */ !function(a,b ...
随机推荐
- 菜鸟笔记 -- Chapter 4.7 代码注释与编码规范
4.7 代码注释与编码规范 在程序代码中适当的添加注释可以提高程序的可读性和可维护性.好的编码规范可以使程序更易阅读和理解.下面我们将介绍几种代码注释,以及应该注意的编码规范. 4.7.1 代码注释 ...
- 動態SQL運用實例
動態SQL運用實例 語法8.1.6之前: EXECUTE IMMEDIATE dynamic_sql_string [INTO {define_var1 [, define_var2] ... | p ...
- flask中的request
1.request是什么? 简单来说,它就是flask的封装的一个对象,这个对象包含着前端请求所带的所有信息.既然说它是一个对象,那么它肯定是有一些熟悉,和方法的,下面就来介绍下request里的熟悉 ...
- tablib模块
####tablib基础知识#### tablib是什么我就不说了,网上一大推,我大概就知道能将数据转为某种格式 1.安装tablib模块 pip install tablib 2.安装完毕,就在你要 ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 012-015
女神节快乐鸭,大学的女生节真的是忙碌呢,到处送礼物,真的是当时男生节的出来混的,总该是要还的hhhhh ------------------------------------------------ ...
- 『ACM C++』PTA浙大 | 基础题 - 打印沙漏
<数据结构>开课前的一些小作业练习,可能因为一个寒假都没有打C++手生了,整个寒假都在帮拍电影做后期特效,导致这道题居然用了两个钟去AC,深感惭愧,作个标记吧,下面上题. 一首好曲推荐:同 ...
- poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...
- 【ntp时间校准配置】
Ntp(网络时间协议)是一种可以通过TCP/IP网络传播,其架构模式可分为C/S(客户端/服务器),PTP(对等),broatcast(广播), mutilbrocast(组播),无论在任何系统或设备 ...
- JS高级. 03 混入式继承/原型继承/经典继承、拓展内置对象、原型链、创建函数的方式、arguments、eval、静态成员、实例成员、instanceof/是否在同一个原型链
继承:当前对象没有的属性和方法,别人有,拿来给自己用,就是继承 1 混入式继承 var I={ }; var obj = { name: 'jack', age:18, sayGoodbye : fu ...
- svg在html的使用
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'> <defs&g ...