清除ListView项目使用事件时创建重复的项目

发布于 2025-01-21 05:08:01 字数 3664 浏览 2 评论 0原文

我有一个列表视图,我希望能够从项目中清除,然后再次填写。
我需要将其与我的引擎类一起使用。

当我清除项目并再次运行时,问题开始重复我的项目:

任何想法为什么会发生以及如何解决?
清除列表视图或初始化新列表视图对我不起作用。

我的表单代码:

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();
        }
    }
}


我的引擎类:


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);
            }
        }
    }
}


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);
            }
        }
    }
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2025-01-28 05:08:01

问题是您每次单击“启动”时都在订阅该事件。

因此,第二次单击“开始”按钮,活动将开火两次,依此类推。

修复它在程序开始时仅订阅一次事件或清除列表时取消订阅

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文