Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   How to check duplication in listbox? (http://www.go4expert.com/forums/check-duplication-listbox-t4120/)

shah123 3May2007 16:43

How to check duplication in listbox?
 
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

shabbir 3May2007 17:19

Re: How to check duplication in listbox?
 
Contains does not work in your situation but you need to be having something to go through the entire list and check.

shah123 3May2007 17:52

Re: How to check duplication in listbox?
 
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"

shabbir 3May2007 18:01

Re: How to check duplication in listbox?
 
foreach(lvt.SubItems in scheduleListView.Items) you need to take the strings in the items and not the subitems.

shah123 3May2007 18:07

Re: How to check duplication in listbox?
 
What you mean?

shabbir 3May2007 18:11

Re: How to check duplication in listbox?
 
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.

shah123 3May2007 19:33

Re: How to check duplication in listbox?
 
Can you write sample of code please?

shabbir 3May2007 20:14

Re: How to check duplication in listbox?
 
foreach(string itemtext in scheduleListView.Items) // change it to subitems or as needed.

shah123 4May2007 14:19

Re: How to check duplication in listbox?
 
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

shabbir 4May2007 14:22

Re: How to check duplication in listbox?
 
scheduleListView.Items.Count as well as scheduleListView.Items != null check should be sufficient


All times are GMT +5.5. The time now is 18:30.