用户写作时,如何在文本框中附加数据

时间:2015-08-18 12:53:30

标签: c# search

我是新来的,所以请放轻松。我需要在c#中使用一个文本框,当用户输入内容时,该文本框会自动附加相关数据。

1 个答案:

答案 0 :(得分:2)

.NET中的标准TextBox支持AutoCompletion。 MSDN Documentation中有一个工作示例。

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);

}