Results 1 to 40 of 40

Thread: [RESOLVED] How fast should a Listview add 150 items

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Resolved [RESOLVED] How fast should a Listview add 150 items

    I'm just trying to do a reality check. I'm loading 150 items into a Listview and it's taking about 3 seconds to do it. I'm loading all the files from a directory and getting their icons or bitmaps, then adding them to the Listview.

    I've read that using "add.range" is faster, but I don't know how to do that with my code. Here's a bit of code so maybe you will understand better how I'm doing things...
    Code:
                For Each FileInfo In dirInfo.GetFiles
                    If FileInfo.Name = "Desktop.ini" Then Continue For
    
                    filename = Path.Combine(DesktopPath, FileInfo.Name)
    
                    If isLink(filename) Then
                        If IsDir(fileInfo) Then
                            bmp = CType(Customize.pb1.Image, Bitmap)
    
                            AddListviewItems(ImageList1, fileInfo, bmp)
                            i += GetOne
                            Continue For
    
                        End If
    
                        extension = Path.GetExtension(fileInfo.FullName)
                        If extension = ".lnk" Then
                            filename = Path.GetFileNameWithoutExtension(fileInfo.FullName)
                            extension = Path.GetExtension(filename)
                        End If
    
                        Select Case extension.ToLower
                            Case ".jpeg", ".jpg", ".pdf", ".png", ".gif", ".ico", ".mp3", ".mp4", ".db", ".csv",
                                 ".xml", ".config", ".rtf", ".sys", ".bmp", ".wav", ".ani", ".r2skin", ".bat",
                                 ".chm", ".adt", ".css", ".js", ".pkgdef", ".vssettings", ".resx", ".natvis",
                                 ".cs", ".vstemplate", ".settings", ".vbproj", ".csproj", ".tt", ".xaml", ".ruleset",
                                 ".xsd", ".xslt", ".wsf", ".cpp", ".h", ".props", ".targets", ".ttf", ".cat",
                                 ".cmd", ".reg", ".c", ".inf", ".ts", ".mht", ".def", ".hpp", ".msi", ".idl",
                                 ".vsix", ".mrimg", ".theme", ".msstyles", ".inc", ".psd1", ".psm1", ".ps1",
                                 ".ps1xml", ".srt", ".aac", ".adt", ".adts", ".accdb", ".accde", ".accdr", ".accdt",
                                 ".aif", ".aifc", ".aiff", ".aspx", ".avi", ".cda", ".dif", ".doc", ".docm", ".docx",
                                 ".dotx", ".eml", ".eps", ".iso", ".jar", ".m4a", ".mdb", ".mid", ".midi", ".mov",
                                 ".mpeg", ".mpg", "potm", "potx", ".ppam", ".ppsm", ".ppsx", ".pptm", ".pptx",
                                ".psd", ".pst", ".pub", ".sldx", ".swf", ".wma", ".wmv", ".xla", ".xlam", ".xlsm",
                                ".xlsmx", ".xltm", ".xltx", ".xps", ".sql", ".fnt", ".fon", ".otf", ".tif", ".tiff",
                                ".asp", ".jsp", ".part", ".php", ".xhtml", ".opd", ".pps", ".ppt", ".class", ".java",
                                ".ods", ".xlr", ".xls", ".xlsx", ".cur", ".adp", ".cpl", ".sfx"
    
                                AddListviewItems(ImageList1, fileInfo, GetBitmap(fileInfo))
    
                                i += GetOne
                                Continue For
    
                            Case ".zip", ".7z", ".rar"
                                AddListviewItems(ImageList1, fileInfo, GetZipBitmap(fileInfo))
                                i += GetOne
                                Continue For
    
                            Case ".pdf"
                                AddListviewItems(ImageList1, fileInfo, GetBitmap(fileInfo))
                                i += GetOne
                                Continue For
    
                            Case ".txt", ".log", ".md"
                                AddListviewItems(ImageList1, fileInfo, GetTextIcon())
                                i += GetOne
                                Continue For
    
                            Case Else
                                Try
                                    AddListviewItems(ImageList1, fileInfo, GetBitmap(fileInfo))
                                    i += GetOne
                                    Continue For
                                Catch ex As Exception
                                    MsgBox(ex.Message)
                                End Try
    
    
                                If extension <> ".dll" Then
                                    AddListviewItems(ImageList1, fileInfo, My.Resources.RedQuestion)
                                    i += GetOne
                                    Continue For
                                End If
    
                                extension = Path.GetExtension(fileInfo.FullName).ToLower
                                If extension = ".lnk" Then
                                    extension = Path.GetFileNameWithoutExtension(fileInfo.FullName)
                                End If
                                If extension.Contains("(") Then
                                    filename = filename.Substring(0, filename.Length - 4)
                                End If
                                If extension <> ".zip" And extension <> ".7z" And extension <> ".rar" Then
    
                                    If Path.GetExtension(fileInfo.FullName).ToLower <> ".bmp" Then
                                        Dim key As String = GetAssociatedProgram(Path.GetExtension(fileInfo.FullName).ToLower)
                                        If key = vbNullString And (Path.GetExtension(fileInfo.FullName.ToLower) <> ".exe") Then
                                            ' this catches files that are not associated with a program...
                                            Try
                                                If Not AddListviewItems(ImageList1, fileInfo, GetBitmap(fileInfo)) Then
                                                    myIcon = ExtractIcon(fileInfo)
                                                    AddListviewItems(ImageList1, fileInfo, myIcon.ToBitmap)
    
                                                    i += GetOne 'new
    
                                                    If myIcon IsNot Nothing Then myIcon.Dispose()
                                                    Continue For
                                                End If
                                            Catch ex As Exception
                                                While myIcon IsNot Nothing
                                                    myIcon.Dispose()
                                                End While
                                                Continue For
                                            End Try
                                        Else
                                            myIcon = ExtractIcon(fileInfo)
                                            AddListviewItems(ImageList1, fileInfo, myIcon.ToBitmap)
    
                                            If myIcon IsNot Nothing Then myIcon.Dispose()
                                            Continue For
                                        End If
                                    End If
                                End If
                        End Select
    ...
    There's more code, but just wanted to give you enough to understand how I'm doing things. To me, 3 seconds seems slow and I would like to make it faster if I can. With the code I have, how can I use Add Range?

    Thanks in advance for any help...

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Instead of adding each item to your listview one by one, add them to a List(of ListViewItem). When you have added all of your items to the list...

    ListView1.Items.AddRange(yourList.ToArray())

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Have a look at the different overloads for creating a ListViewItem, and adding imageindex using With...

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    The problem with your code is that the ListView repaints every time you add an item, using AddRange it just repaints once

  5. #5
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    558

    Re: How fast should a Listview add 150 items

    You can also add before and after the loop ListView.BeginUpdate and ListView.EndUpdate calls so it will not repaint on each added item.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Thank you Paul, I will see if I can get the List working. And thank you peterst, I am using the begin and end update.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Don't give me the answer yet... I think I almost have it figured out, lol

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    I take that back... didn't work

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Ok. What does each item contain? Can you post your AddListviewItems code? Addrange should be no problem...

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Sure, it's a bit big though...
    Code:
        Private Function AddListviewItems(imgList As ImageList, fi As FileInfo, Optional bmp As Bitmap = Nothing,
                                          Optional thisIcon As Icon = Nothing) As Boolean
    
            Dim extension As String = vbNullString
            Dim filename As String = vbNullString
            Dim NormalFilename As String = vbNullString
            Dim fullpathtoFile As String = vbNullString
    
            If fi Is Nothing Then Return False
    
            If Path.GetExtension(fi.FullName).ToLower = ".lnk" Then
                If bmp IsNot Nothing Then
                    If IsDir(fi) Then
                        If bmp IsNot Nothing Then
                            If IsDir(fi) Then
                                If bmp IsNot Nothing Then
                                    imgList.Images.Add(fi.FullName, bmp)
                                    ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
                                    Return True
                                End If
                            End If
    
                            imgList.Images.Add(fi.FullName, bmp)
                            ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                            If bmp IsNot Nothing Then bmp.Dispose()
                            Return True
                        End If
                    Else
                        If bmp IsNot Nothing Then
                            Try
                                imgList.Images.Add(fi.FullName, bmp)
                                ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                                If bmp IsNot Nothing Then bmp.Dispose()
                            Catch ex As Exception
                                MsgBox("Error on file: " & fi.Name & " error is: " & ex.Message)
                            End Try
                            Return True
                        End If
                    End If
    
                    imgList.Images.Add(fi.FullName, bmp)
                    ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                    If bmp IsNot Nothing Then bmp.Dispose()
                    Return True
                Else
                    If thisIcon IsNot Nothing Then
                        imgList.Images.Add(fi.FullName, thisIcon.ToBitmap)
                        ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                        If thisIcon IsNot Nothing Then thisIcon.Dispose()
                        Return True
                    End If
                End If
            Else
                extension = Path.GetExtension(fi.FullName).ToLower
                Select Case extension
                    Case ".zip", ".7z", ".rar"
                        imgList.Images.Add(fi.FullName, bmp)
                        ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                        If bmp IsNot Nothing Then bmp.Dispose()
                        Return True
                    Case Else
    
                        If thisIcon IsNot Nothing Then
                            imgList.Images.Add(fi.FullName, thisIcon.ToBitmap)
                            ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                            If thisIcon IsNot Nothing Then thisIcon.Dispose()
                            Return True
                        Else
                            If bmp IsNot Nothing Then
                                imgList.Images.Add(fi.FullName, bmp)
                                ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                                If bmp IsNot Nothing Then bmp.Dispose()
                                Return True
                            End If
                        End If
                End Select
            End If
    
            'extension = Path.GetExtension(fi.FullName).ToLower
            If extension = ".txt" Then
    
                imgList.Images.Add(fi.FullName, bmp)
                ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                If bmp IsNot Nothing Then bmp.Dispose()
                Return True
            Else
                Select Case extension.ToLower
                    Case ".png", ".zip", ".vb"
    
                    Case Else
                        If bmp IsNot Nothing Then
                            imgList.Images.Add(fi.FullName, bmp)
                            ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
                        Else
                            imgList.Images.Add(fi.FullName, thisIcon)
                            ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
                        End If
    
                        If bmp IsNot Nothing Then bmp.Dispose()
                        Return True
                End Select
            End If
    
            If Path.GetExtension(fi.FullName).ToLower = ".pdf" Then
                If thisIcon IsNot Nothing Then
                    imgList.Images.Add(fi.FullName, Customize.PictureboxPDF.Image)
                    ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                    If thisIcon IsNot Nothing Then thisIcon.Dispose()
                    Return True
                Else
                    imgList.Images.Add(fi.FullName, thisIcon)
                    ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                    Return True
                End If
            End If
    
            If Path.GetExtension(fi.FullName).ToLower = ".png" Then
                imgList.Images.Add(fi.FullName, bmp)
                ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                If bmp IsNot Nothing Then bmp.Dispose()
                Return True
            End If
    
            extension = Path.GetExtension(fi.FullName).ToLower
            If extension = ".vb" Then
                imgList.Images.Add(fi.FullName, thisIcon)
                ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                If thisIcon IsNot Nothing Then thisIcon.Dispose()
                Return True
            End If
    
            If Path.GetExtension(fi.FullName).ToLower = ".zip" Or Path.GetExtension(fi.FullName).ToLower = ".7z" _
                     Or Path.GetExtension(fi.FullName).ToLower = ".rar" Then
    
                If isLink(fi.FullName) Then
                    imgList.Images.Add(fi.FullName, bmp)
                    ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                    If bmp IsNot Nothing Then bmp.Dispose()
                    Return True
                Else
                    imgList.Images.Add(fi.FullName, bmp)
                    ListView1.Items.Add(Trimfilename(fi), fi.FullName).Tag = fi.FullName
    
                    If bmp IsNot Nothing Then bmp.Dispose()
                    Return True
                End If
            End If
    
            Return False
    
        End Function
    Edit: Please overlook the first part of the code. While it does work, I just now noticed how "nuts" it is, lol. Must have been a late night when I wrote that...

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Try the List(of ListViewItem) I suggested...
    Declare the List at Form level. To add an item...

    YourList.Add(New ListViewItem(New String() {aString, anotherString, etc}) With {.Tag = whatever, .ImageIndex = anInteger})

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Thanks, will check that out...

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    I've never need to get an index from an ImageList before, but here's what I'm trying...
    Code:
           myIndex = imgList.Images.IndexOfKey(fi.FullName)
           DoList.Add(New ListViewItem(New String() {filename}) _
                            With {.Tag = fi.FullName, .ImageIndex = myIndex})
    MyIndex is returning 7 (which sounds about right), but when I hover over ".Tag" it says the value is "Nothing". However if I hover over "fi.fullname", it does have the value it should.

    Is this the right way to get an index?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Sorry autocorrect error... edited

    Try Loading the full list, then adding it to the ListView as I showed you...

    ListView1.Items.AddRange(DoList.ToArray())

    It looks like you’re testing the tag with execution stopped before that line is executed. Try putting the breakpoint after that line...

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Oops, I think you are right about me having it on the breakpoint. I have defined "DoList" at the from level and I'm using the same code you put in your last message. But the item doesn't show up in the list view.

    In a way it's kind of a special case. It's a link to a folder. I'm going to try it with a regular file and see what happens

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    I almost have it. Give me a few minutes and I'll show you what I did...

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    If your ListView View isn’t details, looks like you’re using icons? That’d be a slightly different overload of the ListViewItem...

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    I "think" I have it working. At least on some items.
    Here's what the code is now...
    Code:
            Try
                imgList.Images.Add(fi.FullName, bmp)
                filename = Path.GetFileNameWithoutExtension(fi.Name)
    
                myIndex = imgList.Images.IndexOfKey(fi.FullName)
                DoList.Add(New ListViewItem(New String() {filename}) _
                                 With {.Tag = fi.FullName, .ImageIndex = myIndex})
    
                If bmp IsNot Nothing Then bmp.Dispose()
                
                Catch ex As Exception
                     MsgBox("Error on file: " & fi.Name & " error is: " & ex.Message)
                End Try
    Then at the end of the calling sub...
    Code:
       ListView1.Items.AddRange(DoList.ToArray())

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    If your ListView View isn’t details, looks like you’re using icons? That’d be a slightly different overload of the ListViewItem...

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Have a look at...

    ListViewItem(String, Int32)

    Instead of...

    ListViewItem(String())

    https://docs.microsoft.com/en-us/dot..._System_Int32_

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    DoList.Add(New ListViewItem(filename, myIndex) With {.Tag = fi.FullName})

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    However, it's taking just as long to go through all the files. I'm pretty sure adding Images is what's taking up the time now. And I don't know how to get to the bitmap (or index) without adding the images.

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Where are the images coming from? You do know you can load an image list at design time, right?

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Are they file icons? I’m not sure how you can significantly speed that up with a dynamic list of files

  25. #25

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Yes they are file icons. Can't do it at design time because I never know what file will be in the directory. I've been trying everything I can find to speed up the code, and I've been doing it for a while now. It may end up being that "this is as fast as it's going to get".

    But because I'm stubborn, I will keep looking for was to speed things up. This is the last major issue I have to deal with with my program. Sure would love to find a way to make it faster, lol.

    We can give up on this one, if you want. I'll just keep looking...

  26. #26

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Quote Originally Posted by .paul. View Post
    If your ListView View isn’t details, looks like you’re using icons? That’d be a slightly different overload of the ListViewItem...
    The view is LIST

  27. #27
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Yeah i read the earlier posts in this thread. The only way you can significantly increase the speed of loading 150 icons is to load them into your imagelist at design time. The icons are definitely the slow part...

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    The imagelist key could just be the extension...

  29. #29

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Ok then. I really appreciate all the help Paul. I did learn something thanks to you. Thanks a bunch. If I ever find a work around, I'll let you know about it. I'm going to mark this as "resolved".

  30. #30

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Quote Originally Posted by .paul. View Post
    The imagelist key could just be the extension...
    I don't understand what you mean?

  31. #31
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    At the moment, you’re using the filename. If you had a preloaded imagelist, the txt icon would have the key .txt instead of filename.txt

  32. #32

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    This may sound dumb, but how could I have a "preloaded" ImageList for items that are unknown?

  33. #33

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    I changed the number of items in the Listview to 75 and now it loads in 1.4 seconds

  34. #34

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    But I do agree about the preloaded ImageList. I'm sure it would be much faster

  35. #35
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Quote Originally Posted by jumper77 View Post
    This may sound dumb, but how could I have a "preloaded" ImageList for items that are unknown?
    You know the basic file types, even if you don’t have a 100% loaded imagelist, it’d still be faster only downloading 1 or 2 instead of all of the icons. Just a suggestion, but I don’t see it being quicker any other way. You know the windows file explorer is a just-in-time implementation. Only the folders navigated to are loaded as and if needed. You might be able to do something along those lines...

  36. #36
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How fast should a Listview add 150 items

    Not downloading... reading. I’m suggesting you extract all of your images with a small program written purely for that, then load your imagelist at design time from the images you’ve extracted previously. If you click on your imagelist at design time, check the properties. You can add images either at design time, or programmatically as your current code does.

  37. #37

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    Quote Originally Posted by .paul. View Post
    Not downloading... reading. I’m suggesting you extract all of your images with a small program written purely for that, then load your imagelist at design time from the images you’ve extracted previously. If you click on your imagelist at design time, check the properties. You can add images either at design time, or programmatically as your current code does.
    I hope I'm not frustrating you too much because I am being a bit dumb about this. My apologies. Are you saying that if I create an ImageList at design time and "don't" add any images at design time, it would make for faster reading by the program?

    But I don't think that's what you mean. Since every file has it's own unique Icon, I see of no way to add anything at design time.

    Here's one of my functions for getting a file's bitmap. Maybe that will help both of us
    Code:
       Private Function GetBitmap(fi As FileInfo) As Bitmap
            Dim filename As String = vbNullString
            Dim bmp As Bitmap
            Dim ret As Object
            Dim extension As String = vbNullString
    
            ret = rk.GetValue("BitmapIconPath")
            If CStr(ret) <> "" Then
                Return New Bitmap(CStr(ret))
            Else
                extension = Path.GetExtension(fi.FullName)
                filename = ResolveShortcut(fi.FullName)
    
                If IsDir(fi) Then
                    Return CType(Customize.pb1.Image, Bitmap)
                End If
    
                If filename <> "" Then
                    'extension = Path.GetExtension(filename)
                    Select Case extension.ToLower
                        Case ".exe"
                            Using myIcon As Icon = ExtractIcon(fi)
                                Return myIcon.ToBitmap
                            End Using
    
                        Case Else
                            Try
                                Using thisbmp As Bitmap = New Bitmap(16, 16, PixelFormat.Format16bppRgb555)
                                    tmpbmp = thisbmp
                                    tmpbmp = Image.FromFile(filename)
                                    Return CType(tmpbmp, Bitmap)
                                End Using
                            Catch ex As Exception
                                Using myIcon As Icon = ExtractIcon(fi)
                                    Return myIcon.ToBitmap
                                End Using
                            End Try
                            Return bmp
                    End Select
                Else
                    extension = Path.GetExtension(fi.Name)
                    Select Case extension.ToLower
                        Case ".ico"
                            Using myIcon As Icon = ExtractIcon(fi)
                                Return myIcon.ToBitmap
                            End Using
                        Case Else
                            filename = Path.GetFileName(fi.FullName)
    
                            Dim FirstParam As String = filename.Substring(filename.Length - 3, 1)
                            Dim SecondParam As String = filename.Substring(filename.Length - 1, 1)
    
                            If FirstParam = "(" And SecondParam = ")" Then
                                extension = filename.Substring(filename.Length - 8, 4)
                                filename = fi.FullName.Substring(0, fi.FullName.Length - 4)
    
                                Select Case extension.ToLower
                                    Case ".png"
                                        Dim thisbmp As Bitmap = New Bitmap(24, 24, PixelFormat.Format16bppRgb555)
                                        thisbmp = CType(Image.FromFile(fi.FullName), Bitmap)
                                        Return thisbmp
                                End Select
                            Else
                                Try
                                    filename = fi.FullName
                                    filename = ResolveShortcut(filename)
                                    Using Newbmp As Bitmap = New Bitmap(16, 16, PixelFormat.Format16bppRgb555)
                                        tempBmp = Newbmp
                                        If filename <> "" Then
                                            tempBmp = Image.FromFile(Path.Combine(DesktopPath, filename))
                                        Else
                                            tempBmp = Image.FromFile(Path.Combine(DesktopPath, fi.Name))
                                        End If
    
                                        Return CType(tempBmp, Bitmap)
                                    End Using
                                Catch ex As Exception
                                    Using myIcon As Icon = ExtractIcon(fi)
                                        If myIcon Is Nothing Then
                                            Return My.Resources.RedQuestion
                                        Else
                                            Return myIcon.ToBitmap
                                        End If
                                    End Using
                                End Try
                            End If
                    End Select
                End If
    
                Return Nothing
            End If
        End Function
    I also allow the user to customize icons for certain extensions (like zip files). I have a directory for each of those file types that contain about 20 images they can select from (or than can add their own). And they can also just use the default image.

    The customized image choices are stored in the registry and close to the top of the function, you can see where I'm trying to get those variables.

    Don't know if any of this helps, but I hope it does. Let me know one way or another and I will close this item if you think we should

  38. #38
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: How fast should a Listview add 150 items

    Might be worth having a look at https://docs.microsoft.com/en-us/vis.../?view=vs-2019 to see where the problem lies.

    Personally I suspect it is the GetBitmap function but unless you do some profiling you are really guessing at the problem. Once you have performance monitored the app and identified the bottleneck then you can start to look at how to address the issue e,g, caching bitmaps, multi-threading, etc.

  39. #39

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How fast should a Listview add 150 items

    I have to agree with you. And thanks for the link. I will check it out. I'm going to mark this as resolved and if something comes up later, I will start a new thread.

  40. #40

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How fast should a Listview add 150 items

    One last update. I got the time down to 1.8 seconds for 100 items. And I think I can live with that. Also, I don't think there are too many people who have that many items on their desktop

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