【问题标题】:How to update Listview (Windows Forms)?如何更新 Listview(Windows 窗体)?
【发布时间】:2021-03-03 20:09:00
【问题描述】:

在 ListView 中,我提取了一些特定的行。我可以在 MessageBox 中正确显示输出。但是如何在我的 ListView 中更新和显示输出?非常感谢您的帮助!

ArrayList listing = new ArrayList();
for (int i = 0; i < listView1.Items.Count; i++)
{
  int columnNumb = 0; 
  string columnOne= " ";
  string columnTwo = " "; 
  columnOne += listView1.Items[i].Text;
  columnNumb += int.Parse(listView1.Items[i].SubItems[1].Text);
  columnTwo += listView1.Items[i].SubItems[2].Text;
            
  if(columnNumb >= 5)
  {
     listing.Add($"{ columnOne } , {columnNumb} , {columnTwo}");
  }
                
}
        
  StringBuilder sb = new StringBuilder();
  foreach (string line in listing)                      
  sb.AppendLine(line.ToString());
  MessageBox.Show(sb.ToString());




enter image description here

【问题讨论】:

标签: c# winforms listview


【解决方案1】:

也许这篇文章就是你要找的https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls?view=netframeworkdesktop-4.8#:~:text=InvokeRequired%20property%2C%20which%20compares%20the,different%2C%20it%20calls%20the%20Control

总而言之,您要做的是调用函数,在我的情况下,您希望使用参数设置值字符串 b:

SetList(b);

在函数范围之外声明一个委托,如下所示:

delegate void SetListViewCallBacks(string yourtext);

以及在每个回调中设置列表视图项的函数:

private void SetList(string yourtext)
{
    // InvokeRequired required compares the thread ID of the
    // calling thread to the thread ID of the creating thread.
    // If these threads are different, it returns true.
    if (this.listView1.InvokeRequired)
    {
        SetListViewCallBacks d = new SetListViewCallBacks (SetList);
        this.Invoke(d, new object[] { yourtext });
    }
    else
    {
        this.listView1.Items.Add(yourtext);

    }
} 

我还没有测试过,但我的逻辑是这会像这样工作。

【讨论】:

  • 这应该做得更简单。如果您想了解更多信息,请查看上面的以下图片。再次感谢您的帮助,“CodeWarrior”!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
相关资源
最近更新 更多