c# - How to check if an item isn't in a control using a foreach? -


i trying use foreach loop check if listviewitem not in listview , if not write again. code far.

    private void button1_click(object sender, eventargs e)     {         textreader reader = new stringreader(richtextbox1.text);         string[] stritems = null;         foreach (listviewitem item in listview1.items)         {             stritems = reader.readline().split("-".tochararray());             item.text = stritems[0].tostring();             item.subitems.add(stritems[1].tostring());             item.subitems.add(stritems[2].tostring());             item.subitems.add(stritems[3].tostring());             item.subitems.add(stritems[4].tostring());             listview1.items.add(item);         }     } 

all appreciated!

you may read of lines begin with, using file.readalllines. then, can remove duplicates using linq's .distinct() extension:

private void button1_click(object sender, eventargs e) {     string[] lines = file.readalllines(richtextbox1.text);      foreach (string line in lines.distinct())     {         listview1.items.add(new listviewitem(line.split({'-'})));     } } 

i used listviewitem's more convenient constructor.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -