0

I have an array with country names in a listBox. When I enter the textBox, I want any country that starts with what's in the textBox to display.

So if I enter : B => Brazil

Not like this: A => Argentina, England

Only if it starts with what's in the textBox. Full words would also work.

The arraylist contains more than just names, but the code below extracts just the names. List2 is the arraylist I want to use for the search.

 private void textBox7_TextChanged(object sender, EventArgs e)
    {
        listBox1.ClearSelected();
        listBox1.DataSource = null;
       foreach (Country name2 in Mytree.List)
        {
            List2.Add(name2.name);
            Console.WriteLine(List2);
        }
    }
1
  • It is not clear what you want to do. You want to add to your listbox all the country names that starts with your typed text in the TextBox? By the way are you just trying to help your end use to select a particular country without typing all of its name? Have you tried to use the Autocomplete feature of the TextBox? Commented Apr 23, 2017 at 12:59

3 Answers 3

1

If your objective is to avoid typing the full country name then there is no need to reinvent a new kind of user interface. The TextBox has already all the plumbing available to do what you are trying to reproduce with your code. All you need is a source of your data and the settings a pair of properties

// Create the list to use as the custom source. 
var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
{
    "Argentina",
    "England",
    "Brazil",
    "Italy",
    "..."
});

textBox1.AutoCompleteCustomSource = source;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
Sign up to request clarification or add additional context in comments.

Comments

0

Not quite sure but you will want to look at setting the datasource of the listbox to something like

listBox1.DataSource = Mytree.List.Where(a=>a.name.StartsWith(textBox7.Text)

Your example of using a foreach loop to add each item is a little redundant.

4 Comments

This works fine for the first char. What about if I wanted it to check each char of the word?
Thats not true, if you put this on the TextChanged Event it would load the combobox with all values in the Mytree.List where the items "name" property StartsWith whatever value is in textbox.
What if there was Chile and China, StartsWith would give me both of them. How can I narrow the search even more? StartsWith only looks at the first char.
But that is exactly what you have asked for "When I enter the textBox, I want any country that starts with what's in the textBox to display.". "Starts With" in your own words. If you want something different then update your question with more detail on what you want.
0

private void textBox7_TextChanged(object sender, EventArgs e)
{
    listBox1.ClearSelected();
    listBox1.DataSource = null;
    var matchingCountries = Mytree.List.Where(l=>l.name.StartsWith(textBox7.Text));
    foreach (Country name2 in matchingCountries)
    {
            listBox1.Items.Add(name2.name);
    }
}

1 Comment

This almost works, excpet it creates duplicates. So the word Brazil creates 6 instances of Brazil as I spell it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.