简体   繁体   English

Select Listbox 中的所有项目,通过 BackgroundWorker 将它们全部添加到 Listview

[英]Select all items from Listbox, add them all only once to a Listview through BackgroundWorker

I am having some problems lately with selecting ALL items( ONLY once.) from a listbox and adding them to a listview.我最近在从列表框中选择所有项目(一次)并将它们添加到列表视图时遇到了一些问题。 I am using a backgroundworker to handle this task due to big content the listview will contain and to avoid GUI freezing while performing this task.我正在使用后台工作人员来处理此任务,因为列表视图将包含大量内容,并在执行此任务时避免GUI 冻结。

Ok, so here is the BackgroundWorker_ProgressChanged code:好的,这里是 BackgroundWorker_ProgressChanged 代码:

  For Each item In ListBox3.SelectedItems

    listView1.Items.Add(ListBox3.SelectedItem, ImageList1.Images.Count - 1).SubItems.Add("Test")
      ListView1.Items("Test").SubItems.Add("")
       Next
      For Each item As ListViewItem In ListView1.SelectedItems

      Next
     End Sub

The above written code displays items in listview, but ONLY if the user selects a certain item from the Listbox3 and displays infinite times the selected items from the listbox, I want it display ONLY ONCE all selected items from the Listbox, in the Listview.上面编写的代码在列表视图中显示项目,但只有当用户从 Listbox3 中选择某个项目并无限次显示列表框中的选定项目时,我希望它只显示一次列表框中的所有选定项目,在 Listview 中。 I want to select ALL items automatically, without user intervention, I have tried several methods which have failed.我想自动 select ALL项目,无需用户干预,我尝试了几种失败的方法。 Can someone please provide a solution to this issue?有人可以提供这个问题的解决方案吗? Thanks.谢谢。

I just tested and it seems that getting items from a ListBox on a secondary thread is not an issue, so I was wrong about that.我刚刚测试过,似乎从辅助线程上的ListBox获取项目不是问题,所以我错了。 Adding/setting items definitely would be though, so you'd need to add the items to the ListView on the UI thread.但是肯定会添加/设置项目,因此您需要将项目添加到 UI 线程上的ListView中。 Here's some example code that just worked for me:这是一些对我有用的示例代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim outputItems As New List(Of ListViewItem)

    For Each inputItem As String In ListBox1.Items
        outputItems.Add(New ListViewItem(inputItem))
    Next

    e.Result = outputItems
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Dim items = DirectCast(e.Result, List(Of ListViewItem))

    ListView1.Items.AddRange(items.ToArray())
End Sub

The problem is all the screen redrawing.问题是所有的屏幕重绘。 I am guessing you may not need a background worker if you limit the screen redraw.如果您限制屏幕重绘,我猜您可能不需要后台工作人员。

I used a list for the ListViewItems since I don't know how many there will be.我为ListViewItems使用了一个列表,因为我不知道会有多少。 Had to convert the list to an array to use .AddRange .必须将列表转换为数组才能使用.AddRange

As a matter of fact just the .BeginUpdate and .EndUpdate might speed things up enough to be acceptable.事实上,只有.BeginUpdate.EndUpdate可能会加快速度,以至于可以接受。

I am not sure about the use of ImageList so you might have to play around with that.我不确定ImageList的使用,所以你可能不得不玩弄它。

Private Sub OpCode()
    Dim items = From itm In ListBox1.Items 'or ListBox1.SelectedItems
                Select CStr(itm)
    Dim lvItems As New List(Of ListViewItem)
    Dim imageIndex As Integer = ImageList1.Images.Count - 1
    For Each item In items
        Dim lvItem As New ListViewItem(item, imageIndex)
        lvItems.Add(lvItem)
    Next
    'ListView1.BeginUpdate()
    ListView1.Items.AddRange(lvItems.ToArray)
    'ListView1.EndUpdate()
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM