自定义具有自动完成功能的ComboBox
2009-10-30 15:00
363 查看
根据你输入的内容自动过滤出匹配的列表项,并实时显示出来,根据网上的多个文章试验成功,现将代码贡献出来。
原理:设计一个自定义组件,组件继承之combobox,并且拥有一个listbox成员,当组件的text改变时,就从combobox中过滤出匹配的列表项,并且把这些列表项加入到listbox中显示出来。在.net 3.5 vs2008 c#下调试成功。
原理:设计一个自定义组件,组件继承之combobox,并且拥有一个listbox成员,当组件的text改变时,就从combobox中过滤出匹配的列表项,并且把这些列表项加入到listbox中显示出来。在.net 3.5 vs2008 c#下调试成功。
using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Collections; using System.Windows.Forms; namespace ComBoxtest { public partial class AutoComboBox2 : System.Windows.Forms.ComboBox { private System.Windows.Forms.ListBox lbMain; public AutoComboBox2() { InitializeComponent(); } public AutoComboBox2(IContainer container) { container.Add(this); InitializeComponent(); lbMain = new ListBox(); lbMain.Click += new EventHandler(lbMain_Click); lbMain.KeyDown += new KeyEventHandler(lbMain_KeyDown); lbMain.Visible = false; this.Name = "AutoComboBox2"; this.Sorted = true; this.KeyDown += new System.Windows.Forms.KeyEventHandler(AutoComboBox2_KeyDown); this.DropDown += new System.EventHandler(AutoComboBox2_DropDown); this.TextChanged += new System.EventHandler(AutoComboBox2_TextChanged); this.SelectedIndexChanged += new System.EventHandler(AutoComboBox2_SelectedIndexChanged); this.Leave += new System.EventHandler(AutoComboBox2_Leave); } 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; } 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 AutoComboBox2_DropDown(object sender, System.EventArgs e) { lbMain.Visible = false; } private void AutoComboBox2_Leave(object sender, System.EventArgs e) { if (!lbMain.Focused) lbMain.Visible = false; } private void AutoComboBox2_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 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 AutoComboBox2_SelectedIndexChanged(object sender, System.EventArgs e) { lbMain.Visible = false ; } private void AutoComboBox2_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; } } } protected override void OnPaint(PaintEventArgs pe) { // TODO: 在此添加自定义绘画代码 // 调用基类 OnPaint base.OnPaint(pe); } } }
相关文章推荐
- 另一个具有自动完成功能的ComboBox
- 具有自动完成功能的ComboBox(源码、原创)
- 具有自动完成功能的ComboBox
- 下拉框自动完成(autocomplete)带有分页功能SutaraLumpur-jquery.ajaxComboBox.js的使用
- BCB TComboBox自动完成功能
- wxpython comboBox 自动完成提示功能
- ComboBox自动完成功能的例子
- easyui combobox开启搜索自动完成功能的实例代码
- 下拉框自动完成(autocomplete)带有分页功能SutaraLumpur-jquery.ajaxComboBox.js的使用
- BCB TComboBox自动完成功能
- easyui combobox开启搜索自动完成功能
- ExtJs ComboBox 在IE 下 自动完成功能无效的解决方案
- 使 Edit Controls 具有自动完成功能。
- BCB TComboBox自动完成功能
- 具有自动完成功能的TextBox控件
- easyui combobox开启搜索自动完成功能
- C# ComboBox自动完成功能的示例
- ExtJs ComboBox 在IE 下 自动完成功能无效的解决方案
- C# ComboBox自动完成功能的示例
- comboBox 自动完成搜索功能