C#创建自定义自动完成文本框
我想创建一个文本框,其中自动完成下拉菜单显示一些建议。
当然,我想在我的文本框上使用AutoCompleteCustomSource,但问题是,文本框会自动过滤所有不包含输入文本的内容。C#创建自定义自动完成文本框
例如,如果我输入“listen”,我的算法会将“listen(now)”,“listen(later)”和“listen to AAA”作为建议。当我把它们放在autocompletectec源码中时,一切正常。但是,只要我写“现在”,以便文本变为“立即听”,则自动完成下拉列表为空,因为自动完成套件源中的所有项都不以“立即听”开头。
我接下来尝试的是将输入从文本框更改为组合框,在这里我将建议放入Items属性中,然后以编程方式打开下拉列表。这里的问题是,当我打开代码下拉菜单时,第一个项目会自动选中,第一个项目的文本会替换输入的文本。想象一下第一个例子:当你输入“listen”时,下拉菜单打开项目“listen(now)”,“listen(later)”和“listen to AAA”。但组合框中的文本会自动更改为第一项,从而变成“listen(now)”,并且不能输入其他任何内容。
这是我使用的时刻代码:
private void comboBox2_KeyUp(object sender, KeyEventArgs e)
{
string asd = comboBox2.Text;
if (asd.Length < 3)
return;
if (e.KeyCode == Keys.Enter)
{
OpenItem(asd);
return;
}
if (AllToString(comboBox2.Items).Contains(asd))
{
return;
}
DateTime started = DateTime.Now;
System.Threading.Thread tth = new System.Threading.Thread((System.Threading.ThreadStart)delegate()
{
JsonData dat = new JsonData();
//Query autocomplete
...
//End Query
comboBox2.Invoke((MethodInvoker)delegate()
{
if (comboBox2.Tag == null || ((DateTime)comboBox2.Tag) < started)
{
comboBox2.Items.Clear();
comboBox2.Items.AddRange(li.ToArray()); //li is the list of suggestions
comboBox2.Select(comboBox2.Text.Length, 0);
comboBox2.Tag = started;
if (li.Count != 0)
comboBox2.DroppedDown = true;
else
{
comboBox2.Focus();
comboBox2.Select(comboBox2.Text.Length, 0);
}
}
});
});
tth.IsBackground = false; tth.Start();
}
所以我的问题是:我怎么可以创建一个文本或组合框在那里我可以把我的建议,在下拉菜单中,在不改变输入的文本并没有过滤。我希望所有建议都能始终显示。
感谢您的帮助,亚历克斯
更好的是创建一个新的类组合框的哪个herite并重写事件
public class myCombo : ComboBox
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
}
我做一些事来改变显示..要放电网,但很久以前。
尝试在此搜索。
谢谢,我会试试看。 – alex 2012-03-18 01:02:12
使用文本框,尝试通过检索总是相同的建议列表,而不用通过用户输入过滤它们。 – 2012-01-08 00:36:43