具有自动完成功能的ComboBox
来源:互联网 发布:读取通达信日线数据 编辑:程序博客网 时间:2024/06/10 00:11
根据你输入的内容自动过滤出匹配的列表项,并实时显示出来,和IE地址栏类似。
程序很简单,只是很长事时间没更新blog了,用这篇小文章来更新一下。
程序原理是这样的:组件继承之combobox,并且拥有一个listbox成员,当组件的text改变时,就从combobox中过滤出匹配的列表项,并且把这些列表项加入到listbox中显示出来。
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace XPangLib { /// <summary> /// AutoComboBox 的摘要说明。 /// </summary> public class AutoComboBox : System.Windows.Forms.ComboBox { private System.Windows.Forms.ListBox lbMain; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public AutoComboBox() { // 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); lbMain = new ListBox() ; lbMain.Click+=new EventHandler(lbMain_Click); lbMain.KeyDown+=new KeyEventHandler(lbMain_KeyDown); lbMain.Visible = false ; } /// <summary> /// 得到匹配给定字符串的列表 /// </summary> private ArrayList GetFillList(string strValue) { ArrayList alReturn = new ArrayList() ; int iCount = this.Items.Count ; int iLen = strValue.Length ; for(int i = 0 ;i < iCount ;i++) { string strSelItem = this.Items[i].ToString() ; if (strSelItem.Length < iLen) continue ; if (strSelItem.Substring(0,iLen) == strValue) alReturn.Add(strSelItem) ; } return alReturn ; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if( components != null ) components.Dispose(); } base.Dispose( disposing ); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() {// // AutoComboBox // this.Sorted = true; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.AutoComboBox_KeyDown); this.DropDown += new System.EventHandler(this.AutoComboBox_DropDown); this.TextChanged += new System.EventHandler(this.AutoComboBox_TextChanged); this.SelectedIndexChanged += new System.EventHandler(this.AutoComboBox_SelectedIndexChanged); this.Leave += new System.EventHandler(this.AutoComboBox_Leave); } #endregion protected override void OnPaint(PaintEventArgs pe) { // TODO: 在此添加自定义绘画代码 // 调用基类 OnPaint base.OnPaint(pe); } private void lbMain_Click(object sender, System.EventArgs e) { if (lbMain.SelectedItems.Count == 0) return ; string strSel = lbMain.SelectedItem.ToString() ; this.Text = strSel ; int iSel = this.FindStringExact(Text) ; if (iSel != -1) this.SelectedIndex = iSel ; lbMain.Visible = false ; } private void AutoComboBox_TextChanged(object sender, System.EventArgs e) { if (Text == "") { lbMain.Visible = false ; return ; } if (!this.Parent.Controls.Contains(lbMain)) { lbMain.Width = this.Width ; lbMain.Height = 100 ; lbMain.Parent = this.Parent ; lbMain.Top = this.Top + this.Height + 1 ; lbMain.Left = this.Left ; this.Parent.Controls.Add(lbMain); lbMain.BringToFront() ; } ArrayList alFill = GetFillList(Text) ; lbMain.Items.Clear() ; lbMain.Items.AddRange(alFill.ToArray()) ; if (lbMain.Items.Count > 0) lbMain.SelectedIndex = 0 ; if (!lbMain.Visible) lbMain.Visible = true ; } private void AutoComboBox_Leave(object sender, System.EventArgs e) { if (!lbMain.Focused) lbMain.Visible = false ; } private void AutoComboBox_DropDown(object sender, System.EventArgs e) { lbMain.Visible = false ; } private void AutoComboBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (lbMain.Visible) { if (e.KeyCode == Keys.Up||e.KeyCode == Keys.Down||e.KeyCode == Keys.Left|| e.KeyCode == Keys.Right||e.KeyCode == Keys.PageDown||e.KeyCode == Keys.PageUp) { lbMain_KeyDown(lbMain,e) ; e.Handled = true ; } else if (e.KeyCode == Keys.Enter) { lbMain_Click(lbMain,e) ; e.Handled = true ; } } } private void lbMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Up||e.KeyCode == Keys.Left||e.KeyCode == Keys.PageUp) { if (lbMain.SelectedIndex > 0) lbMain.SelectedIndex = lbMain.SelectedIndex - 1 ; } else if (e.KeyCode == Keys.Down||e.KeyCode == Keys.Right||e.KeyCode == Keys.PageDown) { if (lbMain.SelectedIndex < lbMain.Items.Count - 1) lbMain.SelectedIndex = lbMain.SelectedIndex + 1 ; } } private void AutoComboBox_SelectedIndexChanged(object sender, System.EventArgs e) { lbMain.Visible = false ; } }//end class }//end namespace
if (strSelItem.Substring(0,iLen) == strValue)
改成
if (strSelItem.Substring(0,iLen).ToLower() == strValue.ToLower())
可以进行忽略大小写的模糊匹配。
- 具有自动完成功能的ComboBox
- 另一个具有自动完成功能的ComboBox
- 自定义具有自动完成功能的ComboBox
- 具有自动完成功能的ComboBox(源码、原创)
- C# ComboBox自动完成功能的例子
- ComboBox自动完成功能的例子
- ComboBox自动完成功能的例子
- C# ComboBox自动完成功能的示例
- C# ComboBox自动完成功能的示例
- comboBox 自动完成搜索功能
- 具有edit功能的combobox
- 具有edit功能的combobox
- 使 Edit Controls 具有自动完成功能。
- ComboBox自动完成功能(Source)
- wxpython comboBox 自动完成提示功能
- 继承 System.Windows.Forms.ComboBox 实现支持自动完成功能的 ComboBox!个人感觉还可以!!!
- 章鱼哥出品_VB.NET ComboBox、 TextBox 实现自动完成、自动过滤 模糊查询的功能
- ExtJs ComboBox 在IE 下 自动完成功能无效的解决方案
- 类装入问题解密(三)- 处理更少见的类装入问题
- 如何给DropDownList控件添加边框[整理]
- 类装入问题解密(四)-死锁和约束
- 06/01/22
- 你会讲故事吗?
- 具有自动完成功能的ComboBox
- SourceTo 1.0发布
- 功能增强的进度条控件
- 使用C#进行点对点通讯和文件传输(通讯基类部分)
- 使用C#进行点对点通讯和文件传输(发送接收部分)
- 银行不买国库券么
- dotnet中的错误处理
- 可定制的数据库备份和恢复程序
- Marco Cantu: What about Delphi 2006 trial and personal?