private void Form1_Load(object sender, EventArgs e){DataTable dt = new DataTable();dt.Columns.Add("id",typeof(string));dt.Columns.Add("title", typeof(string));DataRow dr = dt.NewRow();dr[0] = "ALL";dr[1] = "全部";dt.Rows.Add(dr);for (int i = 0; i < 10000; i++){DataRow ndr = dt.NewRow();ndr[0] =(i+1);ndr[1] = "测试"+(i+1);dt.Rows.Add(ndr);}List<Record> records = new List<Record>(){ new Record() {id="A001",title="张三",age=1},new Record() { id = "A002", title = "李四", age = 2 } ,new Record() { id = "A003", title = "王五", age = 3 },new Record() {id="A004",title="赵设定",age=1},new Record() { id = "A005", title = "钱发", age = 2 } ,new Record() { id = "A006", title = "孙老弟", age = 3 },new Record() {id="A007",title="周士",age=1},new Record() { id = "A008", title = "吴零四", age = 2 } ,new Record() { id = "A009", title = "郑少东", age = 3 },new Record() {id="A0010",title="张三",age=1},new Record() { id = "A0011", title = "李四", age = 2 } ,new Record() { id = "A0012", title = "王五", age = 3 },};ucCtrlCombbox2.DataSource = dt;ucCtrlCombbox2.DisplayMember = "title";ucCtrlCombbox2.ValueMember = "id";ucCtrlCombbox2.ToPinYinfield = "title";ucCtrlCombbox1.DataSource = records;ucCtrlCombbox1.DisplayMember = "title";ucCtrlCombbox1.ValueMember = "id";ucCtrlCombbox1.ToPinYinfield = "title";// ucCtrlCombbox1.Indexfield = "title";// this.ucCtrlCombbox1.SelectedIndex = -1;}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;namespace HiauokCtrlLib
{/// <summary>/// 继承ComboBox///  ucCtrlCombbox1.DataSource = dt;///  ucCtrlCombbox1.DisplayMember = "title";///  ucCtrlCombbox1.ValueMember = "id";/// ToPinYinfield 转成拼音的字段 例:ucCtrlCombbox1.ToPinYinfield="title";/// Indexfield 索引字段  例:ucCtrlCombbox1.Indexfield="id,title";/// </summary>public partial class ucCtrlCombbox : ComboBox{public DataTable Tempdt;public string _ToPinYinfield=string.Empty;private string _Indexfield = string.Empty;/// <summary>/// 索引字段,以半角英文逗号分隔的字符串/// </summary>[Description("索引字段, 以半角英文逗号分隔的字符串"),Category("自定义")][DefaultValue(typeof(string),"")]public string Indexfield { get { return _Indexfield; }set { _Indexfield = value; }}/// <summary>/// 转换拼音字段/// </summary>[Description("转换拼音字段"), Category("自定义")][DefaultValue(typeof(string), "")]public string ToPinYinfield{get { return _ToPinYinfield; }set { _ToPinYinfield = value; }}public ucCtrlCombbox(){InitializeComponent();}protected override bool ProcessCmdKey(ref Message msg, Keys keyData){//switch (keyData)//{//    case Keys.Right://        MessageBox.Show("Right");//        break;//    case Keys.Left://        MessageBox.Show("Left");//        break;//    case Keys.Up://方向键不反应//        MessageBox.Show("up");//        break;//    case Keys.Down://        MessageBox.Show("down");//        break;//    case Keys.Space://        MessageBox.Show("space");//        break;//    case Keys.Enter://        MessageBox.Show("enter");//        break;//}return false;//如果要调用KeyDown,这里一定要返回false才行,否则只响应重写方法里的按键.//这里调用一下父类方向,相当于调用普通的KeyDown事件.//所以按空格会弹出两个对话框// return base.ProcessCmdKey(ref msg, keyData);}private void ucCtrlCombbox_KeyDown(object sender, KeyEventArgs e){try{this.Tempdt = ListToDataTable((IList)this.DataSource).Copy();}catch{this.Tempdt = ((DataTable)this.DataSource).Copy();}if (e.KeyData == Keys.Space){//string curSltv = string.Empty;//try//{//    curSltv = this.SelectedValue==null?"":this.SelectedValue.ToString();//}//catch(NullReferenceException ex) { }FmCombobox fmbox = new FmCombobox(this.Tempdt, ToPinYinfield, Indexfield);fmbox.ShowDialog(this);if (!string.IsNullOrEmpty(fmbox.ID)){this.SelectedValue = fmbox.ID;}}}/// <summary>/// C# List转换成DataTable/// </summary>/// <param name="list"></param>/// <returns></returns>public static DataTable ListToDataTable(IList list){DataTable result = new DataTable();if (list.Count > 0){PropertyInfo[] propertys = list[0].GetType().GetProperties();foreach (PropertyInfo pi in propertys){//获取类型Type colType = pi.PropertyType;//当类型为Nullable<>时if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))){colType = colType.GetGenericArguments()[0];}result.Columns.Add(pi.Name, colType);}for (int i = 0; i < list.Count; i++){ArrayList tempList = new ArrayList();foreach (PropertyInfo pi in propertys){object obj = pi.GetValue(list[i], null);tempList.Add(obj);}object[] array = tempList.ToArray();result.LoadDataRow(array, true);}}return result;}}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ToolLib;namespace HiauokCtrlLib
{public partial class FmCombobox : Form{private DataTable dt;private string _ToPinYinfield=string.Empty;private string _id=string.Empty;private string _Indexfield=string.Empty;public string ID{get { return _id; }set { _id = value; }}public FmCombobox(DataTable senderdt,string ToPinYinfield,string Indexfield){InitializeComponent();this.dt = senderdt;this._ToPinYinfield =ToPinYinfield;this._Indexfield =Indexfield;}private void FmCombobox_Load(object sender, EventArgs e){if (!string.IsNullOrEmpty(_ToPinYinfield)){this.dt.Columns.Add("PinYin");for (int i = 0; i < this.dt.Rows.Count; i++){this.dt.Rows[i]["PinYin"] = ToolLib.ChineseToPinyinHelper.ConvertToSP(dt.Rows[i][this._ToPinYinfield].ToString());}}dataGridView1.DataSource = this.dt;//if (sltv.ToLower() != "all" && !string.IsNullOrEmpty(sltv))//{//    textBox1.Text = sltv;//}}private void textBox1_TextChanged(object sender, EventArgs e){DataView dv= this.dt.DefaultView;if (dv != null){string keyword = "";if (!string.IsNullOrEmpty(_Indexfield)){string[] strArr = this._Indexfield.Split(',');for (int i = 0; i < strArr.Length; i++){keyword += string.Format("Convert({0}, System.String) LIKE '%{1}%'", strArr[i], textBox1.Text.ToLower());if (i < strArr.Length - 1){keyword += " or ";}}if (!string.IsNullOrEmpty(_ToPinYinfield)){keyword += " or PinYin like '%" + textBox1.Text.ToLower() + "%'";}}else {for (int i = 0; i < this.dt.Columns.Count; i++){keyword += string.Format("Convert({0}, System.String) LIKE '%{1}%'", this.dt.Columns[i].ColumnName,textBox1.Text.ToLower());if (i < this.dt.Columns.Count - 1){keyword += " or ";}}}dv.RowFilter = keyword;}dataGridView1.DataSource = dv;}private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){if (e.RowIndex>= 0){_id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();// string title = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();}}private void button1_Click(object sender, EventArgs e){if (dataGridView1.RowCount > 0){int i = dataGridView1.CurrentRow.Index;_id = dataGridView1.Rows[i].Cells[0].Value.ToString();}this.Close();}private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e){if (e.RowIndex >= 0){_id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();// string title = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();}this.dt.Dispose();this._ToPinYinfield=null;this.Close();}}
}

源码  combobox重写扩展-C#文档类资源-CSDN下载

combobox重写扩展相关推荐

  1. 方法的重写-扩展父类方法,super对象调用父类方法

    class Animal:def eat(self):print("吃---")def drink(self):print("喝---")def run(sel ...

  2. php 静态方法继承,php 5.2中的类继承:重写扩展类中的静态变量?

    我在Zend框架中对某些内容进行子类化时遇到了这个问题.我的决定是,在完全静止的土地上,你只有一个选择-重新定义继承类中的函数: class Animal { public static $color ...

  3. php combobox,ComboBox(下拉列表框)

    ComboBox(下拉列表框) 扩展自$.fn.combo.defaults.使用$.fn.combobox.defaults重写默认值对象. 下拉列表框显示一个可编辑文本框和下拉式列表,用户可以选择 ...

  4. C#拾遗系列(9):继承、接口、扩展方法、分部类、类操作、Ref and Out、可空类型...

    本文内容: 继承 Equal示例 结构和类 属性 Ref and Out 类操作 扩展方法 接口 可空类型 分部类 1. 继承 using System; using System.Collectio ...

  5. C#参数列表中的this(扩展方法)

    参数列表中this的这种用法是在.NET 3.0之后新增的一种特性---扩展方法.通过这个属性可以让程序员在现有的类型上添加扩展方法(无需创建新的派生类型.重新编译或者以其他方式修改原始类型). 扩展 ...

  6. QML 自定义ComboBox控件

    ComboBox是填充数据模型,数据模型通常是JavaScript数组,ListModel或者是整数,但是也支持其他类型的数据模型提供的属性. QtQuick在Qt 5.7在Controls 2.5版 ...

  7. 一文快速入门分库分表中间件 Sharding-JDBC (必修课)

    书接上文 <一文快速入门分库分表(必修课)>,这篇拖了好长的时间,本来计划在一周前就该写完的,结果家庭内部突然人事调整,领导层进行权利交接,随之宣布我正式当爹,紧接着家庭地位滑落至第三名, ...

  8. 一文快速入门分库分表中间件 Sharding-JDBC

    一.Sharding-JDBC 简介 Sharding-JDBC 最早是当当网内部使用的一款分库分表框架,到2017年的时候才开始对外开源,这几年在大量社区贡献者的不断迭代下,功能也逐渐完善,现已更名 ...

  9. 无线轮播android,Android无限轮播Banner的实现

    概述 应用首页的广告轮播Banner,一般都会使用ViewPager来实现,但是ViewPager 没有轮播效果. 现成有这么几种实现方案: 1.使用Integer.MAX_VALUE ,理论上很难达 ...

最新文章

  1. 领跑交互新时代 蓦然认知助力传统产业智能化升级
  2. 基于矩阵分解的推荐算法
  3. live555点播服务器流程深入分析(一)
  4. .NET Socket服务编程之-高效连接接入编
  5. python打包exe 之打包sklearn模型中的各种坑及其解决方法。
  6. 《30天自制操作系统》前言、目录、样章欢迎阅读!
  7. hdu 2275 Kiki Little Kiki 1
  8. shared_ptr简介以及常见问题
  9. GIPS语音编解码器家族
  10. Nest,很酷的东西
  11. 【深入理解Java虚拟机学习笔记】第三章 垃圾收集器与内存分配策略
  12. JTS Geometry关系判断和分析
  13. mysql的半同步复制
  14. tongweb自动部署_Tomcat、TongWeb5.0、TongWeb6.0部署solr
  15. 五款服装连锁店进销存软件排名推荐
  16. C#学习笔记五——选择文件、文件夹操作
  17. 二进制转换为十六进制数是_将二进制数制转换为十六进制数制
  18. mysql主从怎么确保数据一致_如何保证主从复制数据一致性
  19. bxt是什么意思,是什么意思
  20. MySQL 怎么插入10天前的日期_Mysql笔记

热门文章

  1. NPM配置阿里下载源
  2. Altium Designer 19 生成钢网文件
  3. ROS-ROS中的坐标系管理系统
  4. php:Notice: Use of undefined constant id - assumed 'xxxx' 解决办法
  5. 天梯赛基础题型详解(2019 - 08 - 12)
  6. Android 自定义 View:包含多种状态的下载用圆形进度条
  7. php7.2 如何安装imagick扩展
  8. 简单了解 sandbox 沙盒
  9. 灰太狼的数据世界(四)
  10. 血色先锋队(BFS)