1

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:

  • 1|Rui|Lisboa|rui@hotmail.com|912345324|14/01/2000|89564352|Empresa
  • 2|Henrique|Evora|henrique@hotmail.com|914445324|17/05/2001|55464352|Particular
  • 3|Andre|Agueda|andre@hotmail.com|932415374|12/11/1996|23456743|Particular
  • 4|Pedro|Aveiro|pedro@hotmail.com|965342163|30/03/2002|98645372|Empresa

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.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Look at your code, please; especially the first few code lines of your _carrSug_ method... (more specifically, that 3rd code line there saying `sug = null;`) –  Nov 09 '18 at 20:53
  • I tried to removed it but it doens´t work aswell... – Rui Liberal Nov 09 '18 at 20:57
  • Get yourself some time and read some tutorials which teach how to create and work with arrays. It will be time well spent. :-) –  Nov 09 '18 at 20:58
  • I fixed the problem with the array. But I don´t get the autocomplete in the textBox. Any suggestion? – Rui Liberal Nov 09 '18 at 21:19

1 Answers1

0

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.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Ok, Thank you, I´ll be more carefull :) – Rui Liberal Nov 09 '18 at 22:02
  • 1
    @RuiLiberal Please read my edit because you have the code in the `textBoxCliente_TextChanged` which is not ideal and a bad idea. And since you are new, please read [this](https://stackoverflow.com/help/someone-answers). – CodingYoshi Nov 09 '18 at 22:07
  • So my program is doing what I whant but I have a problem and I don´t know how to fix it. When I write on the textBox I keep getting this error: **"System.AccessViolationException: 'Attempt to read or write to protected memory. This is often an indication that other memory is corrupt.'"** . You know why it is happening? – Rui Liberal Nov 10 '18 at 10:19
  • I have no idea. See if [this](https://stackoverflow.com/questions/4074585/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-ot) helps. You can also ask a new question with the new problem if you want and hopefully someone could help you. – CodingYoshi Nov 10 '18 at 14:46