-
Dec 30th, 2017, 01:54 PM
#1
Thread Starter
Addicted Member
Listview - Add Items Flicker
hi i have a question when i add items to my listview it flickers i have the double buffer set to true in the load event of the form i tried the beginupdate
endupdate but no success still flickers any ideas ?
Code:
Public Function ProcessJunkFiles(ByVal L As ListView, ByVal Path As String)
Try
Dim imageList As New ImageList
Dim ListIcon As String = ("C:\Micro Cleaner\AppIcons\Shield.png")
If DirExists(ListIcon) Then
imageList.Images.Add(Bitmap.FromFile(ListIcon))
L.SmallImageList = imageList
End If
Dim list As List(Of String) = ScanjunkFiles(Path)
' Loop through and display each path.
For Each Item As String In list
If Not IsSpecialFolder(Item) And Not IsFileInUse(Item) Then
Dim arr As String() = New String(4) {}
Dim itm As ListViewItem
'Cancel Scan
If CancelScan = True Then
Form1.ToolStripStatusLabel1.Text = UpdateStatusBar("StopStatus")
Form1.ToolStripStatusLabel2.Text = UpdateStatusBar("ClearStatus")
Exit Function
End If
'add items to ListView
arr(0) = AbsoluteFilePath(Item)
arr(1) = FormatBytes(Item)
arr(2) = FilePathExt(Item)
arr(3) = Item
itm = New ListViewItem(arr, 0)
L.BeginUpdate()
L.Items.Add(itm)
L.Items(L.CheckedItems.Count).Checked = True
L.EndUpdate()
End If
Next
Catch ex As Exception
End Try
End Function
-
Dec 30th, 2017, 02:18 PM
#2
Re: Listview - Add Items Flicker
If you are adding multiple items to the ListView using a loop, you need to place the BeginUpdate and EndUpdate statements outside of the loop.
-
Jan 2nd, 2018, 02:29 PM
#3
Re: Listview - Add Items Flicker
Yes, you should only do .SuspendLayout once... add ALL your items, THEN do .ResumeLayout once at the end...
-tg
-
Jan 2nd, 2018, 08:56 PM
#4
Re: Listview - Add Items Flicker
Or, even better, don't add items in a loop. Inside the loop, add your items to a List(Of ListViewItem) and then, after the loop, add them all in a single batch by calling AddRange. That is what the documentation says you should do, after all.
-
Jan 2nd, 2018, 09:21 PM
#5
Re: Listview - Add Items Flicker
Or, use a virtualized ListView, that way only the visible items are rendered.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jan 6th, 2018, 03:21 PM
#6
Re: Listview - Add Items Flicker
Originally Posted by jmcilhinney
Or, even better, don't add items in a loop. Inside the loop, add your items to a List(Of ListViewItem) and then, after the loop, add them all in a single batch by calling AddRange. That is what the documentation says you should do, after all.
Are you sure about this? For all we know AddRange could be simply be a looping against the IEnumerable interface and adding the items one by one using the Add or Insert method. This would be no different to doing the looping yourself. I've always wondered about this but never bothered to investigate since I never really needed to know to answer to this.
[EDIT]
Just looked into the reference source to try and find an answer to this. The answer is still a bit unclear as the ListView's list is actually a collection class that contains an inner list which is declared as an interface. That interface has an AddRange method but being an interface, we can only guess at the implementation. However, I also found an internal ListView collection class which also has an AddRange method that is implemented by making calls to the ListView's methods. This implementation does do a BeginUpdate before adding. But I can't really tell what fits where since it's all so complicated. Would probably take a couple hours to figure out how it all fits together. Chances are, AddRange might be more efficient.
Last edited by Niya; Jan 6th, 2018 at 03:43 PM.
-
Jan 6th, 2018, 05:19 PM
#7
Re: Listview - Add Items Flicker
Have I missed something obvious? Beginupdate suspends the drawing until the Endupdate. In my eyes both these methods are pointless here as after each new item the control will be redrawn. The same as not even including them. Looping each item;
Code:
L.BeginUpdate()
L.Items.Add(itm)
L.Items(L.CheckedItems.Count).Checked = True
L.EndUpdate()
is forcing the listview to be drawn each new item hence flickering....
I look forward to seeing what obvious thing i missed as just driven 3 hours there and back picking up mrs from airport
-
Jan 6th, 2018, 11:19 PM
#8
Re: Listview - Add Items Flicker
Yeah, if you do it inside the loop, it's the same as not using them at all... that's why the suggestion was to move them OUTSIDE the loop.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|