Jump to content
Sign in to follow this  
buymeapc

Quickly populate a listview from a text file

Recommended Posts

buymeapc

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

Share this post


Link to post
Share on other sites
DjDeep00

@buymeapc....I wouldnt use a do loop for this....try using the following...

_FileReadToArray($file,$File_Array)

For $x=1 to $File_Array[0]

;Create your listviewitems
;Update your progressbar

Next

Share this post


Link to post
Share on other sites
buymeapc

thanks, that cut the load time in half. Now, I have to figure out if I can cut the sort time...I may be asking too much, though.

Share this post


Link to post
Share on other sites
DjDeep00

If you mean sorting the listview results...then yes sure you can...just search the forum for listview and sort. There are many UDFs out there but I believe gafrost's udf is pretty fast....

Share this post


Link to post
Share on other sites
weaponx

Personally, I would use _FileReadToArray() to read the info in, then use _ArraySort(), then populate the listview. This would eliminate the need to use the ListView sort.

Share this post


Link to post
Share on other sites
randallc

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

Share this post


Link to post
Share on other sites
buymeapc

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

Share this post


Link to post
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
Sign in to follow this  

×