How to check duplication in listbox?

shah123's Avatar
Contributor
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Contains does not work in your situation but you need to be having something to go through the entire list and check.
0
shah123's Avatar
Contributor
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"
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
foreach(lvt.SubItems in scheduleListView.Items) you need to take the strings in the items and not the subitems.
0
shah123's Avatar
Contributor
What you mean?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
shah123's Avatar
Contributor
Can you write sample of code please?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
foreach(string itemtext in scheduleListView.Items) // change it to subitems or as needed.
0
shah123's Avatar
Contributor
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
scheduleListView.Items.Count as well as scheduleListView.Items != null check should be sufficient