-1

How to read & add all text from a text file to AutoComplete? ( C# Windows Application )

what i want is like:-

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

3 Answers3

1

In your textbox (textBox1) properties set your AutoCompleteMode to what you want, then your source to "Custom" Then load each line or char or whatever is in the file into a string array, and finally use the AddRange function:

string[] colors= new string[] { "Red", "Blue", "Green", "Yellow" }; textBox1.AutoCompleteCustomSource.AddRange(colors);

So for your case, use: StreamReader sr = new StreamReader("somefile.txt");

while ((line = sr.ReadLine()) != null) { //add your lines here to the string array, whatever your criteria may be for each element in the array }

textBox1.AutoCompleteCustomSource.AddRange(/your string array here/);

swabs
  • 515
  • 1
  • 5
  • 19
0

Put this code in your Form_load or any other way so it can be initiated.

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

Hope this help.

zia khan
  • 361
  • 3
  • 14
0

In case of enabled MultiLine for textbox, do uncheck Multiline option and load this code to form load. This will load lines from autocomplete.txt present in project directory.

string[] autosource= File.ReadAllLines(@"autocomplete.txt");
for(int g = 0; g < autosource.Length; g++)
{
    textboxname.AutoCompleteCustomSource.Add(autosource[g]);
}
textboxname.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68