Sign in to follow this  
Followers 0
Laura

Listview

4 posts in this topic

Hello,

I'm using autoit for one week now. I'm new to programming, but i really enjoy playing around with it.

Now, i'm trying to make a script that creates a listview from the data in a text file. The data in the listview should correspond to the data in the textfile, so if it gets updated, the listview should update aswell.

When i run the script my listview items are properly created, but whenever i update the textfile, my array is getting updated, but my listview aint removing the old lines.

_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView)) returns True
_GUICtrlListView_GetItemCount($hListView) returns 0

But in fact nothing gets removed from my listview, and all the old items are displayed.

Bug or just me being stupid? (Probably the last one :) )

What am i doing wrong? Any other approaches to this?

While 1
$lines = _FileCountLines("\\Sharepc\Share\data.txt")
If $lines <> $nrrows Then
$csv = FileRead("\\Sharepc\Share\data.txt")
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF, 2)
$nrrows = UBound($rows)
Dim $entries[$nrrows + 1][5]
For $i = 0 to $nrrows - 1
$temp = StringSplit($rows[$i], ";", 2)
For $j = 0 to UBound($temp) - 1
$entries[$i][$j] = $temp[$j]
Next
Next
ReDim $entries[$nrrows][4]
_ArrayDisplay($entries)
$deleteall = _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
ConsoleWrite($deleteall & @CRLF)
$itemcount = _GUICtrlListView_GetItemCount($hListView)
ConsoleWrite($itemcount & @CRLF)
_GUICtrlListView_AddArray($hListView, $entries)
EndIf
WEnd

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Laura,

There is a problem with the current _GUICtrlListView_DeleteAllItems function as explained in this thread. If you are creating your ListView with the native GUICtrlCreateListView function then that is probably why you cannot delete the items that you have inserted with the UDF _GUICtrlListView_AddArray function. :(

The modified function I posted in that thread should be incorporated into the next release. Until then I suggest you either rewrite the script to use a UDF created ListView - or more easily - use that new function in your script. This example shows you how:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <File.au3>

$hGUI = GUICreate("Test", 500, 500)

$cListView = GUICtrlCreateListView("", 10, 10, 404, 200)
_GUICtrlListView_AddColumn($cListView, "Column 0", 100)
_GUICtrlListView_AddColumn($cListView, "Column 1", 100)
_GUICtrlListView_AddColumn($cListView, "Column 2", 100)
_GUICtrlListView_AddColumn($cListView, "Column 3", 100)

_Load_File("data.txt")

$cButton = GUICtrlCreateButton("Reload", 10, 300, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            _Load_File("data.txt")
    EndSwitch

WEnd

Func _Load_File($sPath)

    ; Read the file into an array
    Local $aLines
    _FileReadToArray($sPath, $aLines)
    ; Create the array to hold the data - we now can size it correctly as the array holds the number of lines
    Local $aEntries[$aLines[0]][4]
    ; Loop through the lines
    For $i = 1 To $aLines[0]
        ; And split each one
        $aItems = StringSplit($aLines[$i], ";")
        ; Now loop through the separate items
        For $j = 1 To $aItems[0]
            ; And add them to the array to load into the ListView
            $aEntries[$i - 1][$j - 1] = $aItems[$j]
        Next
    Next
    ; Remove the curernt items using the new function
    _New_GUICtrlListView_DeleteAllItems($cListView)
    ; And load the new ones
    _GUICtrlListView_AddArray($cListView, $aEntries)

EndFunc   ;==>_Load_File

Func _New_GUICtrlListView_DeleteAllItems($hWnd)
    If _GUICtrlListView_GetItemCount($hWnd) == 0 Then Return True
    If Not IsHWnd($hWnd) Then
        Local $ctrlID
        For $index = _GUICtrlListView_GetItemCount($hWnd) - 1 To 0 Step -1
            $ctrlID = _GUICtrlListView_GetItemParam($hWnd, $index)
            If $ctrlID Then GUICtrlDelete($ctrlID)
        Next
        If _GUICtrlListView_GetItemCount($hWnd) = 0 Then Return True
        $hWnd = GUICtrlGetHandle($hWnd)
    EndIf
    Return _SendMessage($hWnd, $LVM_DELETEALLITEMS) <> 0
    Return False
EndFunc   ;==>_New_GUICtrlListView_DeleteAllItems

I used the following file with 4 elements to each row - it seems to match your code: ;)

Item 00;Subitem 01;Subitem 02;Subitem 03
Item 10;Subitem 11;Subitem 12;Subitem 13
Item 20;Subitem 21;Subitem 22;Subitem 23
Item 30;Subitem 31;Subitem 32;Subitem 33
Item 40;Subitem 41;Subitem 42;Subitem 43

If the data file is not in the correct format let me know and we can change the code to suit. All clear? :)

M23

Edit: Welcome to the AutoIt forum, by the way. :)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank you! Works perfectly

Really like your comments, I've learned a lot from them.

Share this post


Link to post
Share on other sites

Laura,

Seems like the old crystal ball still works! Glad I could help. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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  
Followers 0