清除ListView项目使用事件时创建重复的项目
我有一个列表视图,我希望能够从项目中清除,然后再次填写。
我需要将其与我的引擎
类一起使用。
任何想法为什么会发生以及如何解决?
清除列表视图或初始化新列表视图对我不起作用。
我的表单代码:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您每次单击“启动”时都在订阅该事件。
因此,第二次单击“开始”按钮,活动将开火两次,依此类推。
修复它在程序开始时仅订阅一次事件或清除列表时取消订阅
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