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?
Copyright Notice:Content Author:「NoviceMe」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12306350/combobox-doubling-in-values-c-sharp