C# WinForm 技巧:COMBOBOX搜索提示

comboBox和textBox支持内置的搜索提示功能,

在form的InitializeComponent()中添加如下语句:

//将数据添加到搜索范围
this.comboBox1.AutoCompleteCustomSource.AddRange(new string[] { "10.152.154.89", "10.152.154.90", "10.152.252.10", "10.152.252.11" });

默认是None,还有Append,和SuggestAppend属性,除了默认的None都可以实现搜索提示的功能

this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;

转自http://www.cnblogs.com/luomingui/archive/2010/03/17/1688188.html

但经过测试并不能实现真正的模糊查询,只能从左到右查询,所以还是得自己写一个:

 private void comboBox1_TextUpdate(object sender, EventArgs e)
{
ComboBox cbBox = (ComboBox)sender;
List<string> listNew = new List<string>();
cbBox.Items.Clear();
foreach (var item in listCell) //已有数据
{
if (item.Contains(cbBox.Text))
{
listNew.Add(item);
}
}
cbBox.Items.AddRange(listNew.ToArray());
cbBox.SelectionStart = cbBox.Text.Length;
Cursor = Cursors.Default;
cbBox.DroppedDown = true;
}
上一篇:function [ binary,decimal ] = num2binary16( number )


下一篇:Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别