Results 1 to 8 of 8

Thread: Listview - Add Items Flicker

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    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

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Listview - Add Items Flicker

    Yes, you should only do .SuspendLayout once... add ALL your items, THEN do .ResumeLayout once at the end...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Listview - Add Items Flicker

    Quote Originally Posted by jmcilhinney View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width