Jump to content

Quickly populate a listview from a text file


 Share

Recommended Posts

I'm currently populating a listview using a Do While loop and it takes around 10 seconds to populate the list.

Is there a better alternative?

Thanks

Func StartUp()
    Local $clicks = 0, $info
    _GUICtrlListViewSetColumnWidth ($listview, 0, 93)
    Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($listview) ]
    $num = 1
    Global $strMACAddress
    If FileExists($file) = 0 Then FileWrite($file, "")
    $msg = GUIGetMsg()
    Do
        $line = FileReadLine($file, $num)
        If $line <> "" Then GUICtrlCreateListViewItem($line, $listview)
        $num = $num + 1
        $percent = Round(($num / _FileCountLines($file))*100, 0)
        _GUICtrlStatusBarSetText($StatusBar, "Building list, " & $percent&"% complete")
        GUICtrlSetData($ProgressBar1, $percent)
    Until $line = ""
    _GUICtrlListViewSort($listview, $B_DESCENDING, 0)
    GUISetState(@SW_ENABLE, $main)
    GUICtrlSetState($input, $GUI_FOCUS)
    GUICtrlSetData($ProgressBar1, 0)
    SelectionTotal()
EndFunc
Link to comment
Share on other sites

Hi,

1. I agree with WeaponX

2. Take "_FileCountLines" out of any loop as it reads the whole file each time (assign to var)

3. If only one column, consider "ListBox" instead of listview;

3a then you can do fileread without array, replace "@crlf" with "|" separator, add to LB in one command, no loop, and automatically sorts

4. I f you use LV and ArraySort is still slow, quickest sort on 1D array is via jscript and scripting object (10x)

Let me know..

Best, Randall

Link to comment
Share on other sites

Oh wow, that is so much better!! I commented out the status bar lines and what a difference!

Thanks guys!....Here's the code I used:

Func StartUp()
    GUISetState(@SW_DISABLE, $main)
    Local $File_Array, $num = 1
    _GUICtrlListViewSetColumnWidth ($listview, 0, 93)
    Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($listview) ]
    If FileExists($file) = 0 Then FileWrite($file, "")
    _FileReadToArray($file, $File_Array)
    _ArraySort($File_Array, 0, 1)
    $msg = GUIGetMsg()
    For $x = 1 To $File_Array[0]
        If $File_Array[$x] <> "" Then GUICtrlCreateListViewItem($File_Array[$x], $listview)
    ;$num = $num + 1
    ;$percent = Round(($num / _FileCountLines($file))*100, 0)
    ;_GUICtrlStatusBarSetText($StatusBar, "Building list, " & $percent&"% complete")
    ;GUICtrlSetData($ProgressBar1, $percent)
    Next
;_GUICtrlListViewSort($listview, $B_DESCENDING, 0)
    GUISetState(@SW_ENABLE, $main)
    GUICtrlSetState($input, $GUI_FOCUS)
;GUICtrlSetData($ProgressBar1, 0)
    SelectionTotal()
    GUISetState(@SW_ENABLE, $main)
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...