C#:如何为文本框使用自定义自动完成源

时间:2015-08-29 23:51:36

标签: c#

我想知道如何使用户选择自动完成源,浏览并加载或导入文本文件导入的文本文件按顺序排列在fartextbox或其他东西。如果他想让编辑变得舒适和易于使用,那么即使可能吗?

1 个答案:

答案 0 :(得分:2)

仔细阅读文档https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

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

所以,你需要从某个地方获取建议到string []。它可以是一个字典,txt文件,二进制文件......一旦你将文字输入数组,你就可以了。