How to check duplication in listbox?

Contributor
3May2007,16:43   #1
shah123's Avatar
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
Go4Expert Founder
3May2007,17:19   #2
shabbir's Avatar
Contains does not work in your situation but you need to be having something to go through the entire list and check.
Contributor
3May2007,17:52   #3
shah123's Avatar
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"
Go4Expert Founder
3May2007,18:01   #4
shabbir's Avatar
foreach(lvt.SubItems in scheduleListView.Items) you need to take the strings in the items and not the subitems.
Contributor
3May2007,18:07   #5
shah123's Avatar
What you mean?
Go4Expert Founder
3May2007,18:11   #6
shabbir's Avatar
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.
Contributor
3May2007,19:33   #7
shah123's Avatar
Can you write sample of code please?
Go4Expert Founder
3May2007,20:14   #8
shabbir's Avatar
foreach(string itemtext in scheduleListView.Items) // change it to subitems or as needed.
Contributor
4May2007,14:19   #9
shah123's Avatar
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
Go4Expert Founder
4May2007,14:22   #10
shabbir's Avatar
scheduleListView.Items.Count as well as scheduleListView.Items != null check should be sufficient