【发布时间】:2018-02-05 09:00:28
【问题描述】:
我有一个文本框,用于输入字符串并在此文本框中显示所有可用的结果。当前代码如下:
private void Form_Load(object sender, EventArgs e)
{
TextBox.AutoCompleteMode = AutoCompleteMode.Suggest;
TextBox.AutoCompleteSource = AutoCompeteSource.CustomSource;
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if(t != null)
{
if(t.Text.Length > = 1)
{
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(s.Name);
this.TextBox.AutoCompleteCustomSource = collection;
}
}
}
在上面的代码中,s.Name 是我要搜索的所有字符串的来源。只有我正确输入了字符串的第一个字母,它才会起作用。例如。 s.Name 之一可能是ABCDEF 我希望它在我键入它的任何子字符串时可用,可能是EF 或BC,但不仅仅是AB 或ABC。我该怎么做?谢谢!
【问题讨论】:
-
Contains 似乎是您正在寻找的方法,您可以使用
bool contains = s.Name.Contains(subName, StringComparison.OrdinalIgnoreCase); -
根据您的描述,您应该将
TextBox.AutoCompleteCustomSource设置为s.Name并将TextBox.AutoCompleteMode设置为AutoCompleteMode.Suggest -
感谢您的回复。我重新编辑了我的问题。
-
没有。投票最多的答案没有达到我所描述的。