1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to check duplication in listbox?

Discussion in 'C#' started by shah123, May 3, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    I have the code which i used to check duplication in listbox but not working?

    Q. I have 3 columns in listview grid and i want to check duplication with only one field. How to do that?


    Code:
    if(listView1.Items.Contains(lvi) == false)
    {
    //Add the item to the ListView Control
    listView1.Items.Add(lvi);
    }
    else
    {
    //Warn user of duplicate entry...
    MessageBox.Show("Duplicate Item!");
    }
    
    Thanks, Please help
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Contains does not work in your situation but you need to be having something to go through the entire list and check.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    What would be the best way then? Can you give me the loopong condition here please?
    e.g.
    foreach(lvt.SubItems in scheduleListView.Items)
    //This above give me error "Type and identifier are both required in a foreach statement"
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    foreach(lvt.SubItems in scheduleListView.Items) you need to take the strings in the items and not the subitems.
     
  5. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    What you mean?
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    You need to get the string of the items and then compare with the one you would like to add new and see if it matches.
     
  7. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Can you write sample of code please?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    foreach(string itemtext in scheduleListView.Items) // change it to subitems or as needed.
     
  9. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    This below code doesnt work

    foreach(string itemtext in scheduleListView.Items) // change it to subitems or as needed??

    The main issue is first of all you have to check if the list is null if it is null then just insert the new item when you try to insert item again it will check for duplication. how would you implement that?

    Just help me small bit in looping condition. Thanks Shabbir
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    scheduleListView.Items.Count as well as scheduleListView.Items != null check should be sufficient
     
  11. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    It worked. Thanks added new function to check the duplication
    Code:
    private bool IsInCollection(ListViewItem lvi)
            {
                foreach (ListViewItem item in scheduleListView.Items)
                {
                    bool subItemEqualFlag = true;
                    for (int i = 0; i < item.SubItems.Count; i++)
                    {
                        string sub1 = item.SubItems[i].Text;
                        string sub2 = lvi.SubItems[i].Text;
                        if (sub1 != sub2)
                        {
                            subItemEqualFlag = false;
                        }
                    }
                    if (subItemEqualFlag)
                        return true;
                }
    
                return false;
    
            }
    
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83

Share This Page