ComboBox doubling in values c#
NickName:NoviceMe Ask DateTime:2012-09-07T02:37:07

ComboBox doubling in values c#

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

Answers
Mike Perrenoud 2012-09-06T18:39:19

Add comboBox.Items.Clear() before the AddRange. So the whole block should be.\n\nprivate void nameTextBox_Leave(object sender, EventArgs e) \n{ \n var names = ConfigurationManager.AppSettings.AllKeys \n .Where(k => k.StartsWith(\"name\")) \n .ToArray(); \n\n // Add names to combobox \n comboBox.Items.Clear();\n comboBox.Items.AddRange(names); \n} \n",


infografnet 2012-09-06T18:46:03

i'm not sure if I understand perfectly, but why can't you just clear the items before populating?\n\ncomboBox.Items.Clear()\ncomboBox.Items.AddRange(names);\n\n\nalso you could try not to use Items, but DataSource:\n\ncomboBox.DataSource = names;\n",


More about “ComboBox doubling in values c#” related questions

ComboBox doubling in values c#

I have following piece of code: private void nameTextBox_Leave(object sender, EventArgs e) { var names = ConfigurationManager.AppSettings.AllKeys .Whe...

Show Detail

Avoid doubling in combobox

I have following code that is generating random no. between 1 to 10. This is working fine. But, I do not want doubling in combobox (cmbRnd is my combo box). How can I avoid it? Private Sub cmdRnd...

Show Detail

setCustomLabelFormatter doubling values in label

I got custom formatter that I found in other thread, that helps with setting strings as horizontal label. GraphViewSeries series = new GraphViewSeries(new GraphView.GraphViewData[] { ...

Show Detail

Populate comboBox from listBox values in C#

I can't solve this problem. I need to populate comboBox items from values in listBox (Windows Form in C#). Every time I add values ​​in listbox, combobox automatically has to be populated. Thanks in

Show Detail

JS, doubling the element of an array and check if the next element of the current element that is doubling is equal to the previous element

I need help to solve this exercise: An array is given and composed of whole numbers. Write a function which takes it as a parameter this array and modifies it by doubling the values. If the previous

Show Detail

XML and ComboBox in C#

I'm trying to match selected year in ComboBox dropdown list with national holidays in Ireland, so when the user click on, e.g. 2010, he gets the list of holidays along with their corresponding date...

Show Detail

Access: Combobox Values Depend on Previous Combobox

So I have two comboboxes, Combobox1 and Combobox2. If the user chooses "Apple" for Combobox1, I want the values for Combobox2 to be "Sauce" and "Seeds". Likewise, if a user picks "Blueberry" for

Show Detail

Combobox first values should be null

Using C# & MYSQL Using Combobox in my webpage, in a combobox i want to display a null values first, then it should display all values.. For Example Combobox.item = null values combobox.item ...

Show Detail

if i select combobox values then automatically other combobox values shoulde be view based on first combobox in c# silverlight

namespace combobox { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); comboBox1.Items.Add("TAMIL NADU"); ...

Show Detail

Is there a function to cut a multiple the same values on combobox in c#

PIC I try to show all the values that are not repeated. This is for software that will display a meteorological report private void frm_main_Load(object sender, EventArgs e) {

Show Detail