Solved

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

Posted on 2011-09-06
17
3,621 Views
Last Modified: 2012-06-27
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.
private bool CCDescription_Duplicate(int position)
        {
            //Declare Local Variables
           // bool found = false;
            int counter = 0;
            string temp = "";

            if (comboBox1.Items.Count > 2)
            {
                //temp = comboBox1.Items[comboBox1.Items.Count - 2].ToString();
                while (counter < comboBox1.Items.Count - 1)
                {
                    //if (comboBox1.Items[comboBox1.Items.Count-1] == comboBox1.Items[counter])
                    if(thirdPartyCOM[position] == comboBox1.Items[counter])
                    {
                        return true;
                    }//end if

                    else
                        counter++;

                }//end while
            }//end if

            else if (comboBox1.Items.Count == 2)
            {
                if (comboBox1.Items[0] == thirdPartyCOM[position] || comboBox1.Items[1] == thirdPartyCOM[position])
                    return true;
                //if (comboBox1.Items[0] == comboBox1.Items[1])
                //    return true;
            }//end else if

            else
                return false;
            
            return false;


        }//end CCDescription_Duplicate

Open in new window

0
Comment
Question by:thenthorn1010
  • 6
  • 5
  • 2
  • +2
17 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36489949
wanna c#of this ?
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36489957
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
 

Author Comment

by:thenthorn1010
ID: 36489987
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.
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36490003
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
 

Author Comment

by:thenthorn1010
ID: 36490024
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 hope this information clarifies any other questions that you had.
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36490042
Ok, so one more question, the thirdPartyCOM ArrayList will also contain duplicates if the ComboBox.Items collection does, correct?
0
 
LVL 28

Accepted Solution

by:
strickdd earned 250 total points
ID: 36490083
List<ListItem> newCBList = new List<ListItem>();

foreach(ListItem item in myCBList.Items)
{
     if(newCBList.Contains(item))
           next;
     else
         newCBList.Add(item);
}

myCBList.Items.Clear();

foreach(ListItem item in newCBList)
{
     myCBList.Items.Add(item);
}
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:thenthorn1010
ID: 36490100
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.
0
 
LVL 7

Assisted Solution

by:Gewgala
Gewgala earned 250 total points
ID: 36490146
In that case it sounds like what you want to do is the following:

// create and append to a filtered list
List<string> filteredItems = new List<string>();

foreach (string description in thirdPartyCOM)
{
     if (!filteredItems.Contains(description))
     {
           filteredItems.Add(description);
     }
}

// clear your combobox items
comboBox1.Items.Clear();

// add your combo box items back in, this time from the filtered list
foreach (string description in filteredItems)
{
       comboBox1.Items.Add(description);
}

Open in new window


I suppose it is very similar to what @strickdd posted but slightly different since your thirdPartyCOM also will contain duplicates.
1
 
LVL 7

Expert Comment

by:Gewgala
ID: 36490165
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.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 36490294
Hello, If you fill the combobox through query, you have to use the distinct keyword into the query. It is for remove the duplicate data before filling the combobox.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36490329
Not even an assist on that? Gewgala even points out that my post is pretty much right, just using the wrong object.
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36506868
strickdd should be awarded with an assist on post #36490083.
0
 

Author Comment

by:thenthorn1010
ID: 36510402
strickdd,

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
 

Author Comment

by:thenthorn1010
ID: 36510504
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now