I am currently trying to use a foreach loop to check if a listviewitem is NOT in the listview and if it is to not write it again. This is my code so 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 help is appreciated!
You may as well read all of the lines to begin with, using
File.ReadAllLines
. Then, you can remove duplicates using LINQ's.Distinct()
extension:I also used
ListViewItem
's more convenient constructor.