ListBox

Can someone tell me how to check for duplicate items in my ListBox


Answer this question

ListBox

  • Talbengal

    I tried what you posted, and it seems to me that it only checks if a certain item exists in the listBox. Maybe I described my question wrong Lets say I have 6 items in my listBox :

    687,

    365,

    489,

    687,

    634,

    687

    What I need to do is check the listBox to see if there are duplicate items. In this case, the duplicate item is 687. Do you understand what I am saying.



  • rgdonato

    Ahh you are checking to see if the listbox already contains duplicates... you can do something along the lines of this:

    bool found = false;


    for ( int i = 0; i < listBox.Items.Count; i++ )

    {

    // Reset found
    found = false;

    for ( int j = 0; j < listBox.Items.Count; j++ )

    if ( listBox.Items [ j ] == listBox.Items [ j ] )

    found = true; // Do whatever you want to do with your duplicates here

    }


  • Xavier Divini

    You can check to see if an object exists in your listbox already by doing:

    int index = listbox.Items.IndexOf ( itemToCheckFor )

    if ( -1 == index )
    Item does not exist

  • ListBox