Question

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!

Was it helpful?

Solution

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:

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 also used ListViewItem's more convenient constructor.

OTHER TIPS

I think @minitech's answer missed one piece: you don't just want to add only distinct new entries, you also want to filter out any new entries that are already in the listbox.

private void button1_Click(object sender, EventArgs e) 
{
    string[] lines = richTextBox1.Lines;

    // Not sure about the exact Items.Contains() and item.Text usage, but that's the idea
    foreach (string line in lines.Distinct().Where(line => 
        !listView1.Items.Contains(item => line == item.Text)))
    { 
        string[] items = line.Split('-'); 
        listView1.Items.Add(new ListViewItem(items)); 
    } 
} 

There are ways to optimize this, but choosing the right one will depend on where the listview's items are coming from in the first place (and how many items are in the listview, i.e. how much you need to optimize this).

I'm not sure I understand your question.

If you're searching through all the items that are already in the ListView for a specific match, then why not just use "FindItemWithText()" or equivalent:

If nothing's returned from the query, the item isn't in the list.

'Hope that helps ... at least a little bit...

Is your code only for checking the existing ListViewItem? If you want your code to add a new item to the empty ListView, then it will be failed since it won't go inside the foreach loop if you have nothing in the ListView.

If can try the following for checking the existing ListViewItem

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());
        if (ListView1.FindItemWithText(strItems[0].ToString()) != null)
        {
            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);
        }
    }
}

Please refer to the URL below

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.find(v=vs.85).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top