[英]c# - listview does not add items when using a delegate
Using a worker thread, I am adding items to a ListView, however to do this I have implemented a delegate in the form as shown below in the code sample. 我使用工作线程将项目添加到ListView,但是为此,我以如下代码示例所示的形式实现了一个委托。
Problem: 问题:
When the ListView.Items.add() line is executed, nothing is added to the ListView 当执行ListView.Items.add()行时,不会向ListView添加任何内容
What I have tried: 我尝试过的
I added a button, when clicked, it successfully adds a ListViewItem to the ListView object, as it should. 我添加了一个按钮,当单击该按钮时,它成功将ListViewItem正确添加到了ListView对象。 (this is before the worker thread start, for testing/debugging)
(这是在工作线程启动之前,用于测试/调试)
I have also tried swapping the ListView to a RichEdit in case I had a bug in the code, but the items successfully display in the RichEdit, thus I added another ListView incase the first/previous one was buggy, and without renaming the ListView, I tried adding the items, but without success. 我还尝试将ListView交换到RichEdit,以防万一我在代码中有错误,但是项目成功显示在RichEdit中,因此我添加了另一个ListView,以防第一个/前一个有错误,并且不重命名ListView,我尝试添加项目,但没有成功。
What happens in the Delegate: 代表发生了什么:
Nothing, after the clear() method, the 4 columns each with headers disappear and nothing is added to the list view 什么都没有,在clear()方法之后,带有标题的4列消失了,并且没有任何内容添加到列表视图
Conclusion: 结论:
My only conclusion is that the delegate is not compatible with the ListView, since I was able to add the items to a RichEdit, but not to a ListView. 我唯一的结论是,委托与ListView不兼容,因为我能够将项目添加到RichEdit中,但不能添加到ListView中。
Is this a bug? 这是错误吗? Am I doing something wrong?
难道我做错了什么?
code sample 代码样本
void connected_add_device()
{
string[] t = { "123", "-1", "sa.d.sdf.s.fg", "sda-f-sd-fgds-gf" };
if (display_connected_Devices.InvokeRequired)
display_connected_Devices.BeginInvoke((MethodInvoker)delegate ()
{
display_connected_Devices.Clear();
display_connected_Devices.Items.Add(new ListViewItem(t));
}
);
else
{
display_connected_Devices.Clear();
display_connected_Devices.Items.Add(new ListViewItem(t));
}
}
For what it is worth, I had a bad GIT commit. 为了它的价值,我犯了一个错误的GIT承诺。 After everthing is fixed, this ListView (which before the GIT issue, had successfuly displayed the items) now doesn't display the items anymore.
固定所有内容后,此ListView(在GIT发布之前已成功显示了项目)现在不再显示项目。
Use display_connected_Devices.Items.Clear();
使用
display_connected_Devices.Items.Clear();
instead. 代替。 Because
display_connected_Devices.Clear();
因为
display_connected_Devices.Clear();
will clear, like you noticed, the columns aswell. 也会像您注意到的那样清除列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.