2

I have following piece of code:

private void nameTextBox_Leave(object sender, EventArgs e)
{                  
    var names = ConfigurationManager.AppSettings.AllKeys
                        .Where(k => k.StartsWith("name"))
                        .ToArray();

    // Add names to combobox
    comboBox.Items.AddRange(names);
}

Problem is each time I press Tab from textbox, comboBox elements keep on doubling. If it had Ken, John, Tim in there, it will show that twice if I press tab again.

I tried using distinct in the names above but that does not do anything as new instantance is created each time and previous is saved. I cannot make comboBox empty right after adding names as it is being used in a button click latter on in the code.

Only alternative i thought was of declaring a global variable and make sure its value is 0 and then only insert values in comboBox, and change it to 1 once value is inserted. But that does not seem like a good coding practice.

Is there any better way to get this done?

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
NoviceMe
  • 3,126
  • 11
  • 57
  • 117

3 Answers3

6

Add comboBox.Items.Clear() before the AddRange. So the whole block should be.

private void nameTextBox_Leave(object sender, EventArgs e) 
{                   
    var names = ConfigurationManager.AppSettings.AllKeys 
                        .Where(k => k.StartsWith("name")) 
                        .ToArray(); 

    // Add names to combobox 
    comboBox.Items.Clear();
    comboBox.Items.AddRange(names); 
} 
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
1

i'm not sure if I understand perfectly, but why can't you just clear the items before populating?

comboBox.Items.Clear()
comboBox.Items.AddRange(names);

also you could try not to use Items, but DataSource:

comboBox.DataSource = names;
infografnet
  • 3,749
  • 1
  • 35
  • 35
  • Any particular reason why DataSource should be used rather then items? – NoviceMe Sep 06 '12 at 18:48
  • @NoviceMe, `DataSource` is certainly another option and is generally used when binding to complex objects in conjunction with the display name and value name fields. Under the covers it still produces a list of `Items` based off of the complex object using the display field but works with it differently when providing you a `SelectedValue` because it pulls the value off of the complex object. – Mike Perrenoud Sep 06 '12 at 18:54
  • @NoviceMe, DataSource gives you more flexibility. You can bind any object like List, Dictionary, custom class etc, and use specific member of this object as a DisplayMember and another member as a ValueMember. For example: [how to bind a list to a combobox? (Winforms)](http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms) – infografnet Sep 06 '12 at 19:25
0

The suggestions to clear the combo box first should solve your problem, but if you are loading the combo box on page load as well then I don't think this is the best solution (just masking the real problem). If you are populating the combobox on page load then add a Page.IsPostBack check in there:

example:

if(!Page.IsPostBack)
{
    //Populate the initial values into the combobox
}

If you aren't then the clear solution should be fine.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486