I have a textBox named "textBoxCliente" and I want it to appear suggestions when I writte in there from the .txt file. The suggestions that I want to appear from the .txt file are in the position 1 "parts[1]", each position are separated with the caracter "|".
My .txt file is this:
My code is:
public partial class Vender : UserControl
{
public Vender()
{
InitializeComponent();
}
string dir = (Environment.CurrentDirectory + "/Bd/clientes.txt");
string[] sug = new string[File.ReadAllLines(Environment.CurrentDirectory +
"/Bd/clientes.txt").Count()];
private void textBoxCliente_TextChanged(object sender, EventArgs e)
{
carrSug();
for (int i = 0; i < sug.Length; i++)
{
textBoxCliente.AutoCompleteCustomSource.Add(sug[i]);
}
textBoxCliente.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
private void carrSug()
{
string[] lines = File.ReadLines(dir).ToArray();
int nLine = File.ReadAllLines(dir).Count();
for (int j = 0; j <= nLine - 1; j++)
{
string[] parts = lines[j].Split(new char[] { '|' });
sug[j] = parts[1];
}
}
}
What I did was using the "string[] sug" to save the values of the position 1 of each line and then use it to show the suggestions.
As a programmer, get better at reading carefully . Here is the documentation for AutoCompleteCustomSource
:
Gets or sets a custom System.Collections.Specialized.StringCollection to use when the System.Windows.Forms.TextBox.AutoCompleteSource property is set to CustomSource .
Emphasis Mine
See the bolded part in the above, make sure you do that:
textBoxCliente.AutoCompleteSource = AutoCompleteSource.CustomSource;
Also, you do not need to do that every time the user types. The event handler textBoxCliente_TextChanged
will be called every time the text changes. Instead, put the code in the constructor or in the form's load event.
Some Suggestions
Give your methods meaningful names. For example, carrSug()
is not very meaningful. Plus it does not follow the C# coding conventions--it looks like Java. Also, keep the method cohesive. You are doing some parts of the suggestion in the carrSug()
and then some of it you are doing in textBoxCliente_TextChanged
. Here is a more meaningful method:
private AutoCompleteStringCollection clientSuggestions;
private void LoadClientSuggestions()
{
this.clientSuggestions = new AutoCompleteStringCollection();
string[] suggestionsFromFile = File.ReadLines("YourPath.txt").Select(x => x.Split('|').Skip(1).First()).ToArray();
this.clientSuggestions.AddRange(suggestionsFromFile);
}
The above method uses Ling so make sure to import: using System.Linq;
Here is how to use it (Put this code in your form's constructor or Load method):
this.LoadSuggestions();
this.textBoxCliente.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.textBoxCliente.AutoCompleteCustomSource = this.clientSuggestions;
this.textBoxCliente.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Why did I put the word Load
in the method name? Because, it returns nothing so Load
adds meaning.
Also, stop writing the same string multiple times:
"/Bd/clientes.txt"
Make that a constant so if you need to change it, you change it in one place.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.