Sign in to follow this  
Followers 0
koolmelee

ListView Manipulation

9 posts in this topic

#1 ·  Posted (edited)

I am trying to create a program with a ListView of 3 columns. With the list populated I want to read all the data from the listview into a file(only to be loaded the next time the program runs), with each listview line in a new line in the text file like so:

item1a|item1b|item1c|

item2a|item2b|item2c|

.

.

.

I just do not know how to extract all the data from the listview--I could only figure out how to extract the selected list item with GUICtrlRead(GUICtrlRead($listview)). Is there any way to get this data saved into an array!!?

I already am able to load the data from the file into the listview:

Func setAccountList($listView)
    dim $string
    dim $file
    
    $file = FileOpen("accountList.txt",0)
    If $file = -1 Then
        FileClose($file)
        $file = FileOpen("accountList.txt",2)
        MsgBox(0,"fail","fileread fail")
        FileClose($file)
        $file= FileOpen("accountList.txt",0)
    Else
        Do
            $string=FileReadLine($file)
            if $string=="" then ExitLoop
            GUICtrlCreateListViewItem($string, $listView)   
        Until $string ==""
        FileClose($file)
    EndIf
EndFunc

Many thanks for your help.

Edited by koolmelee

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Take a look at GUIListView management in the UserDefined Functions section of the help file'

In particular _GUICtrlListView_GetItemCount() and _GUICtrlListView_GetItemTextArray()

#Include <GuiListView.au3>
#Include <Array.au3>
$Count = _GUICtrlListView_GetItemCount($listview)
Local $aArray[$Count +1]
For $I = 0 To _GUICtrlListView_GetItemCount($listview)
 $aArray[$i] = _GUICtrlListView_GetItemTextArray($listview, $i)
Next

Each element of $aArray will contain an Array of the text for the associated Item. If you prefer to just have each element contain a "|" delimited string then take a look at _ArrayToString(). Then you would change the line

$aArray[$i] = _GUICtrlListView_GetItemTextArray($listview, $i)

to

$aHold = _GUICtrlListView_GetItemTextArray($listview, $i)
$aArray[$i] = _ArrayToString($aHold, "|")

WARNING: Written on the fly and not tested

Edit: Forgot the #includes

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

If you want to save it to an Ini file for the next time around then use.

#Include <GuiListView.au3>
#Include <Array.au3>
$Ini = @ScriptDir & "\settings.ini"
For $I = 0 To _GUICtrlListView_GetItemCount($listview)
   $aHold = _GUICtrlListView_GetItemTextArray($listview, $i)
   IniWrite($ini, "ListView", $i,  _ArrayToString($aHold, "|"))
Next

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thank you, this works! But how do I read this file now? Sorry, I am a not familiar with ini file reading.

Edited by koolmelee

Share this post


Link to post
Share on other sites

Thank you, this works! But how do I read this file now? Sorry, I am a not familiar with ini file reading.

If FileExists $Ini) Then
   $aItems = IniReadSection($ini, "listView")
   For $i = 1 To Ubound($aItems)-1
      GUICtrlCreateListViewItem($listliew, $aItems[$i][2])
   Next
EndIf

That's assuming that the ListView has no items when you open the script.

Also if there is any chance that the number of ListViewItems will decrease when the script is running then I suggest that you delete the Ini file right after the "Next" statement in the above, then recreate it when you close the script.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If FileExists $Ini) Then
   $aItems = IniReadSection($ini, "listView")
   For $i = 1 To Ubound($aItems)-1
      GUICtrlCreateListViewItem($listliew, $aItems[$i][2])
   Next
EndIf

That's assuming that the ListView has no items when you open the script.

Also if there is any chance that the number of ListViewItems will decrease when the script is running then I suggest that you delete the Ini file right after the "Next" statement in the above, then recreate it when you close the script.

After Some modification of the code:

dim $aItems[99][5], $i, $Ini
    $Ini = @ScriptDir & "\settings.ini"
    If FileExists($Ini) Then
       $aItems = IniReadSection($Ini, "ListView")
       For $i = 1 To Ubound($aItems)-2
            MsgBox(0,"setacclist",$aItems[$i][1])
            GUICtrlCreateListViewItem($aItems[$i][1],$listView)
       Next
    EndIf

Originally it gave Array out of bounds error on the line:

GUICtrlCreateListViewItem($listliew, $aItems[$i][2])---and BTW the parameters are out of order

The .ini file created previously is here

[ListView]
[ListView]
0=3|col1|col2|2133
1=3|col1a|col2b|2133
2=3|col1c|col2c|2133
3=3|||

When the list view is populated at program start, column one gets filled with "3" rather than "col1". I am guessing the "3" indicates the number of columns.

Edited by koolmelee

Share this post


Link to post
Share on other sites

After Some modification of the code:

dim $aItems[99][1], $i, $Ini
    $Ini = @ScriptDir & "\settings.ini"
    If FileExists($Ini) Then
       $aItems = IniReadSection($Ini, "ListView")
       For $i = 1 To Ubound($aItems)-2
            MsgBox(0,"setacclist",$aItems[$i][1])
            GUICtrlCreateListViewItem($aItems[$i][1],$listView)
       Next
    EndIf

Originally it gave Array out of bounds error on the line:

GUICtrlCreateListViewItem($listliew, $aItems[$i][2])---and BTW the parameters are out of order

The .ini file created previously is here

[ListView]
[ListView]
0=3|col1|col2|2133
1=3|col1a|col2b|2133
2=3|col1c|col2c|2133
3=3|||

When the list view is populated at program start, column one gets filled with "3" rather than "col1". I am guessing the "3" indicates the number of columns.

In the code where you write to the ini, make this change

IniWrite($ini, "ListView", $i,  _ArrayToString($aHold, "|", 1))

And in the code you just posted.

GUICtrlCreateListViewItem($aItems[$i][1],$listView)

is correct. The [$i][2] was incorrect ( I know better),

however the Ubound($aItems) -2 should go back to Ubound($aItems)-1 or you will drop the last item.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thank you very much, keep up the good work!

Share this post


Link to post
Share on other sites

NP

I'm just glad it worked, like I said it was written on the fly, without testing and no sleep.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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