【问题标题】:How to read & add all text from a text file to AutoComplete?如何读取文本文件中的所有文本并将其添加到自动完成?
【发布时间】:2012-09-14 22:32:57
【问题描述】:

如何读取文本文件中的所有文本并将其添加到自动完成? (C# Windows 应用程序)

我想要的是这样的:-

foreach (string str in File.ReadAllLines("sometext.txt"))
{
    AutoComplete.Items.Add(str);//this code not works it's just example
}

【问题讨论】:

  • please someone tell me is it possible to ... 是的,这是可能的。
  • 当你尝试它时会发生什么?有用吗?
  • 它不起作用这就是我问的原因。这只是我想做的一个例子。
  • “它不工作”并没有告诉我们任何事情。具体是什么不工作?

标签: c# .net autocomplete io autofill


【解决方案1】:

如果为文本框启用MultiLine,请取消选中Multiline 选项并加载此代码以形成加载。这将从项目目录中的autocomplete.txt 加载行。

string[] autosource= File.ReadAllLines(@"autocomplete.txt");
for(int g = 0; g < autosource.Length; g++)
{
    textboxname.AutoCompleteCustomSource.Add(autosource[g]);
}
textboxname.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

【讨论】:

    【解决方案2】:

    将此代码放入您的 Form_load 或任何其他方式,以便可以启动它。

     string[] autosource= File.ReadAllLines("C:\\autocomplete.txt");
            for(int i = 0; i < autosource.Length; i++)
            {
                txt_searchfiled.AutoCompleteCustomSource.Add(autosource[i]);
            }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      在您的文本框 (textBox1) 属性中将您的 AutoCompleteMode 设置为您想要的,然后将您的源设置为“自定义”然后将文件中的每一行或字符或任何内容加载到字符串数组中,最后使用 AddRange 函数:

      字符串[] 颜色= 新字符串[] { “红色的”, “蓝色”, “绿色的”, “黄色的” }; textBox1.AutoCompleteCustomSource.AddRange(colors);

      因此,对于您的情况,请使用:StreamReader sr = new StreamReader("somefile.txt");

      while ((line = sr.ReadLine()) != null) { //将您的行添加到字符串数组中,无论您的标准可能是数组中的每个元素 }

      textBox1.AutoCompleteCustomSource.AddRange(/你的字符串数组在这里/);

      【讨论】:

      • 这个答案就像一个魅力!可惜它没有被接受。
      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多