我想显示建议的话使用自动完成的文本框。例如,我键入Tod...从我的数据库中的所有单词以“tod”开头将显示,但在我选择一个单词后,如果我键入另一个单词,如“was”,则下面将没有建议的单词。可能是因为字符串集合没有“today was”。我想展示的是仅以“was”而不是“today was”开头的建议单词。我正在做一个项目,预测最常用的单词。这是我的代码
public partial class Form1 : Form
{
string[] arr = new string[] { "sn k", "sn k", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtInput_TextChanged(object sender, EventArgs e)
{
predict();
}
void predict()
{
txtInput.AutoCompleteMode = AutoCompleteMode.Suggest;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
col.AddRange(arr);
txtInput.AutoCompleteCustomSource = col;
}
}
}
2条答案
按热度按时间ua4mk5z41#
所以一个基本的“Roll Your Own”解决方案是这样工作的:
这应该做你想要的,它将显示自动完成每个字!可以添加一些技巧,比如随着文本的移动移动自动完成框。
gstyhher2#
最简单的解决方案是实现一个基于Textbox的自定义控件,其中包含Textbox控件和列表框控件。使用文本更改事件用匹配用户选择的字符串填充列表框。重点仍然是控制不包含在应用程序形式的上下文中的控制。在内部,定义的用户控件必须处理来自包含的文本框和列表控件的任何事件。仅当自动完成列表包含多个匹配项时(根据规则),才显示列表框。
如果只存在一个匹配项,则(通常)在文本框中显示“完成”文本,其中模式为“追加”。如果建议不以“从...开始”为基础,这是不可行的。只有“建议”模式可以用“开始”以外的其他方式实现。在建议模式下,在匹配列表不为空的所有情况下都会显示列表框。