0

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!

Ian Lundberg
  • 1,813
  • 10
  • 31
  • 48

4 Answers4

1

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • This throws the exception 'Illegal characters in path.' on the 'string[] lines = File.ReadAllLines(richTextBox1.Text);' line. – Ian Lundberg Jun 06 '12 at 02:26
  • +1, but you missed part of Ian's request - he also needs to check for items that already exist in the listview. – Lars Kemmann Jun 06 '12 at 02:27
  • 1
    @IanLundberg: Sorry, I misread that as a `StreamReader`, not a `StringReader`. Just use `richTextBox1.Lines`. – Ry- Jun 06 '12 at 02:27
  • @LarsKemmann: Yeah, looking back, I'm not sure I understand the question. We'll see how this works. :| – Ry- Jun 06 '12 at 02:29
1

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).

Lars Kemmann
  • 5,509
  • 3
  • 35
  • 67
0

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...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

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

Nick
  • 1,128
  • 7
  • 12
  • is there a reason why when I try to type 'ListView1.ListViewItemCollection' that ListViewItemCollection doesn't exist? – Ian Lundberg Jun 06 '12 at 02:38
  • If I type ListView.ListViewItemCollection it pops up in intellisense, but if I try to do it with my existing ListView it doesn't and also if I do it this way (ListView.ListViewItemCollect) the .Find method isn't there. – Ian Lundberg Jun 06 '12 at 02:54
  • are you working with a normal ListView or an inherited ListView? – Nick Jun 06 '12 at 03:19
  • I have updated my answer by using FindItemWithText, you can try again – Nick Jun 06 '12 at 03:36