0

I am reading a file and displaying its content in a list view using a timer. Though i use listview.items.clear() every time, my list doesnot clear and the same data is repeated to list every time.

    private void timer1_Tick(object sender, EventArgs e)
    {
        bufferedListView1.Items.Clear();
        StreamReader sr = new StreamReader("C:\\sample.txt");
        string s;
        s = sr.ReadLine();
        while (s != null)
        {
            s = sr.ReadLine();
            var m = Regex.Match(s, @"^([a-zA-Z._]+)@([\d]+)");
            if (m.Success)
            {
                allcont ac = new allcont();
                ac.name = m.Groups[1].Value;
                ac.number = m.Groups[2].Value;
                con.Add(ac);
                s = sr.ReadLine();
            }
        }
        foreach (allcont aa in con)
        {
            ListViewItem i = new ListViewItem(new string[] { aa.name, aa.number });
            i.Tag = aa;
            bufferedListView1.Items.Add(i);
        }
        sr.Close();

    }

    contacts con = new contacts();
    public class contacts:List<allcont>
    { 

    }
    public class allcont
    {
        public string name;
        public string number;
    }

Solution please...

Shiridish
  • 4,942
  • 5
  • 33
  • 64
  • Oh why is it that way? I am doing Items.clear each time right? – Shiridish Aug 04 '12 at 12:20
  • Where is `con` declared --- don't use globals like this. – Hogan Aug 04 '12 at 12:22
  • ok lets suppose for the first time it clears the list, then adds items. Second time the items are cleared again and the at the end I add items, and so on.. This is what I have in my mind. Where am I wrong? – Shiridish Aug 04 '12 at 12:23
  • you have a variable called `con` -- you don't clear this list -- if you want to do what you describe you have to at least clear the list contained in `con` or stop using this global variable. – Hogan Aug 04 '12 at 12:28

2 Answers2

3

It seems like you are not cleaning con container, so it it's content is appended every timer tick (instead of being replaced).

max
  • 33,369
  • 7
  • 73
  • 84
1

This change will fix your problem.

   private void timer1_Tick(object sender, EventArgs e)
    {
        bufferedListView1.Items.Clear();
        StreamReader sr = new StreamReader("C:\\sample.txt");
        contacts con = new contacts();
        string s;
        s = sr.ReadLine();
        while (s != null)
        {
            s = sr.ReadLine();
            var m = Regex.Match(s, @"^([a-zA-Z._]+)@([\d]+)");
            if (m.Success)
            {
                allcont ac = new allcont();
                ac.name = m.Groups[1].Value;
                ac.number = m.Groups[2].Value;
                con.Add(ac);
                s = sr.ReadLine();
            }
        }
        foreach (allcont aa in con)
        {
            ListViewItem i = new ListViewItem(new string[] { aa.name, aa.number });
            i.Tag = aa;
            bufferedListView1.Items.Add(i);
        }
        sr.Close();

    }

    public class contacts:List<allcont>
    { 

    }
    public class allcont
    {
        public string name;
        public string number;
    }
Hogan
  • 69,564
  • 10
  • 76
  • 117