How to Remove Duplicate Items From a Combo Box in C#

I was attempting to write code to remove duplicate items either before they are written to the combobox selection, or removing them once they have been added to the collection so that only one instance of each type of Credit Card Description will appear. I have attached my code that I was attempting to use to complete this task, and i am failing to see where I am "missing the connection" as to why I am unable to successfully remove duplicate items or stop them from being input. Please reference the attached code, or please feel free to give your own example of code on how to loop through all of the elements of a combo box and be able to remove the items from there.

I think the best approach is to stop them before they are input. So, that leads me to my question for you, how are the items input exactly? Is it a textbox that the user types in, and with each new entry an item is added to the combo box?

0

thenthorn1010Author Commented: 2011-09-06

Gewgala,

All of the data is read in through a INI file and is preloaded. (There will be various lists of numbers and duplicate credit card description entries from the data. So, the data is being read into the combobox, where I was attempting to run the code I attached, to remove duplicate items.) The values are entered in manually and can have various values over 300 different locations...so I cannot code around having a specific type of "Decription"...I hope that helps your answer. The code loading the combobox works and it includes all duplicate values...The attached function is supposed to return true if there is a duplicate value in the ComboBox collection.

ok, so all you're looking for is to return a true if it contains a duplicate, or are you wanting your duplicate actually removed at the same time?

Also, is this a winforms app? What is the data type of each item in the combo box? Is it the string value of the CreditCardDescription class?

0

thenthorn1010Author Commented: 2011-09-06

i was just looking for the duplicate to be removed if it was found within the ComboBox collection. The last variable is an ArrayList of type strings that are being compared to the Combox.Items. (In my code, thirdPartyCOM is an ArrayList of strings for the values that are read in from the INI file.)

I apologize if I am a bit confusing. All of the values from the INI file are read into the thirdPartyCOM ArrayList. It does contain duplicate values, and can have multiple duplicate entries for a credit card description. For instance "Chase Bank" could appear in the thirdPartyCOM ArrayList 6 times. I was attempting to loop through all of the values in the thirdPartyCOM, and remove the duplicates by looping through all of the values.

Actually what @strickdd posted should do the trick for you. His example is looping through the combo box items themselves, whereas mine is looping through your thirdPartyCOM ArrayList. Either one should work fine.

I think the main thing though is that it's better to just clear one of them out and then rebuild the combobox from the filtered items that you create. This is much easier then trying to compare two different collections.

I apologize for not giving you any points on the assist. I will reward the points. I also apologize if I confused you on the object that was being used and being asked to be used to solve the question that was being asked.

thenthorn1010

0

thenthorn1010Author Commented: 2011-09-09

The points should be split between strickdd, following post http:#36490083, and Gewgala, post http:#a36490146, with points being shared equally...I apologize to strickdd for not taking full consideration of his proposal, even though it did definitely lead to the final solution.