C# 中有没有办法猜测用户将要输入什么?例如,当您在 Visual Studio 中键入并以 Fi 开头时,File 会作为答案出现并提示用户按 Enter 键输入。有没有办法用文本框做到这一点?
问问题
295 次
1 回答
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);
}
于 2013-06-24T17:18:51.870 回答