Home > Software engineering >  Clearing ListView items create duplicate items when using events
Clearing ListView items create duplicate items when using events

Time:04-25

I have a list view that I want to be able to clear from items and then fill it up again.
I need to use it with events to my Engine class.

The problem starts when I clear the items and run it again, it duplicate my items:
enter image description here

Any idea why it happens and how to solve it?
Clearing the list view or initialize new list view doesn't work for me.

My Form code:

using System;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate void createListViewCallBack(string i_File, string i_FileStatus);
        private void createListView(string i_File, string i_FileStatus)
        {

            if (this.InvokeRequired)
            {
                createListViewCallBack s = new createListViewCallBack(createListView);
                this.Invoke(s, i_File, i_FileStatus);
            }
            else
            {
                ListViewItem item = new ListViewItem(i_File);
                item.SubItems.Add(i_FileStatus);
                listView1.Items.Add(item);
            }
        }


        private void buttonStart_Click(object sender, EventArgs e)
        {
            Engine.BuildListViewUpdate  = Engine_BuildListViewUpdate;
            Engine.BuildList();
            //createListView(@"c:\a.txt", @"yes");
            //createListView(@"c:\b.txt", @"no");
        }


        private delegate void buildListUpdateCallBack(string i_File, string i_FileStatus);
        private void Engine_BuildListViewUpdate(string i_File, string i_FileStatus)
        {
            if (this.InvokeRequired)
            {
                buildListUpdateCallBack s = new buildListUpdateCallBack(Engine_BuildListViewUpdate);
                this.Invoke(s, i_File, i_FileStatus);
            }
            else
            {
                ListViewItem item = new ListViewItem(i_File);
                item.SubItems.Add(i_FileStatus);
                listView1.Items.Add(item);
            }
        }

        private void clear1()
        {
            this.listView1 = new ListView();
            //this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            //this.columnHeader1,
            //this.columnHeader2});
            this.listView1.HideSelection = false;
            this.listView1.Location = new System.Drawing.Point(55, 156);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(633, 265);
            this.listView1.TabIndex = 1;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            //listView1.Clear();
            //clear1();
        }
    }
}


My Engine class:


namespace WindowsFormsApp3
{
    public delegate void BuildListViewEventHandler(string i_File, string i_FileStatus);
    static class Engine
    {
        public static event BuildListViewEventHandler BuildListViewUpdate;

        public static void BuildList()
        {
            BuildListViewUpdate(@"c:\a.txt", @"yes");
            BuildListViewUpdate(@"c:\b.txt", @"no");
        }

        public static void OnBuildListViewUpdate(string i_File, string i_FileStatus)
        {
            if (BuildListViewUpdate != null)
            {
                BuildListViewUpdate.Invoke(i_File, i_FileStatus);
            }
        }
    }
}


CodePudding user response:

The problem is you are subscribing to the event every time you click start.

So the second time you click the start button the event will fire twice and so on.

To fix it subscribe to the event only once at the start of your program or unsubscribe when clearing the list

private void buttonClear_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    Engine.BuildListViewUpdate -= Engine_BuildListViewUpdate;
}
  • Related