C#中有没有办法猜测用户输入的内容?例如,当您在Visual Studio中键入并以Fi开头时,File会出现作为答案并提示用户按Enter键输入它。有没有办法用文本框做到这一点?
答案 0 :(得分:11)
这实际上取决于应用程序的复杂性,但直接的方法是将TextBox上的AutoCompleteMode属性设置为相关的AutoCompleteMode枚举。来自MSDN链接的示例代码
private void Form1_Load(object sender, EventArgs e)
{
// Create the list to use as the custom source.
var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
});
// Create and initialize the text box.
var textBox = new TextBox
{
AutoCompleteCustomSource = source,
AutoCompleteMode =
AutoCompleteMode.SuggestAppend,
AutoCompleteSource =
AutoCompleteSource.CustomSource,
Location = new Point(20, 20),
Width = ClientRectangle.Width - 40,
Visible = true
};
// Add the text box to the form.
Controls.Add(textBox);
}