Jump to content

How to save all the contents of GUICtrlCreateList?


Recommended Posts

Here one easy way :

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <Array.au3>

Example()

Func Example()
  Local $sText, $idListBox

  ; Create GUI
  GUICreate("List Box Sel Item Range", 400, 296)
  $idListBox = GUICtrlCreateList("", 2, 2, 396, 296, BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL))
  GUISetState(@SW_SHOW)

  ; Add strings
  _GUICtrlListBox_BeginUpdate($idListBox)
  For $iI = 1 To 10
    $sText = StringFormat("%03d : Random string ", Random(1, 100, 1))
    For $iX = 1 To Random(1, 20, 1)
      $sText &= Chr(Random(65, 90, 1))
    Next
    _GUICtrlListBox_AddString($idListBox, $sText)
  Next
  _GUICtrlListBox_EndUpdate($idListBox)

  ; Select all items
  _GUICtrlListBox_SelItemRange($idListBox, 0, -1)
  Local $aList = _GUICtrlListBox_GetSelItemsText($idListBox)
  _ArrayDisplay($aList)
  ; Save it to file ?
  Do
  Until GUIGetMsg() = $GUI_EVENT_CLOSE
  GUIDelete()
EndFunc   ;==>Example

 

Link to post
Share on other sites
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$List1 = GUICtrlCreateList("", 16, 8, 321, 318)
For $i = 1 To 100 Step 1
    _GUICtrlListBox_AddString($List1, 'Item ' & $i)
Next
GUISetState(@SW_SHOW)

_ListToFile($List1, @ScriptDir & '\List.txt')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _ListToFile($hList, $sFilePath)
    Local $sText
    Local $hFile
    For $i = 0 To (_GUICtrlListBox_GetCount($hList) - 1) Step 1
        $sText &= _GUICtrlListBox_GetText($hList, $i) & @CRLF
    Next
    $hFile = FileOpen($sFilePath, $FO_OVERWRITE)
    If $hFile = -1 Then
        ConsoleWrite('ERROR: Failed to open "' & $sFilePath & '"' & @CRLF)
    EndIf
    FileWrite($hFile, $sText)
    FileClose($hFile)
EndFunc

 

Link to post
Share on other sites
14 minutes ago, mike2003 said:

I preferred to collect everything in one variable.

It would have been nice if you have mentioned it in the first place.  Another approach :

_GUICtrlListBox_SetSel($idListBox)
  Local $sList = _ArrayToString(_GUICtrlListBox_GetSelItemsText($idListBox), @CRLF, 1)
  MsgBox(0, "", $sList)

 

Link to post
Share on other sites
20 minutes ago, Luke94 said:
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$List1 = GUICtrlCreateList("", 16, 8, 321, 318)
For $i = 1 To 100 Step 1
    _GUICtrlListBox_AddString($List1, 'Item ' & $i)
Next
GUISetState(@SW_SHOW)

_ListToFile($List1, @ScriptDir & '\List.txt')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _ListToFile($hList, $sFilePath)
    Local $sText
    Local $hFile
    For $i = 0 To (_GUICtrlListBox_GetCount($hList) - 1) Step 1
        $sText &= _GUICtrlListBox_GetText($hList, $i) & @CRLF
    Next
    $hFile = FileOpen($sFilePath, $FO_OVERWRITE)
    If $hFile = -1 Then
        ConsoleWrite('ERROR: Failed to open "' & $sFilePath & '"' & @CRLF)
    EndIf
    FileWrite($hFile, $sText)
    FileClose($hFile)
EndFunc

 

Also _FileToList if you need it?

Func _FileToList($sFilePath, $hList)
    Local $hFile
    Local $sText = ''
    Local $aLines
    _GUICtrlListBox_ResetContent($hList)
    Sleep(2000)
    $hFile = FileOpen($sFilePath, $FO_READ)
    If $hFile = -1 Then
        ConsoleWrite('ERROR: Failed to open "' & $sFilePath & '"' & @CRLF)
    EndIf
    $sText = FileRead($hFile)
    If @ERROR Then
        ConsoleWrite('ERROR: Failed to read "' & $sFilePath & '"' & @CRLF)
    EndIf
    FileClose($hFile)
    If StringLen($sText) > 0 Then
        $aLines = StringSplit($sText, @CR, $STR_NOCOUNT)
        For $i = 0 To (UBound($aLines) - 1) Step 1
            If StringLen(StringStripWS($aLines[$i], ($STR_STRIPLEADING + $STR_STRIPTRAILING))) > 0 Then ; Ignore blank lines
                _GUICtrlListBox_AddString($hList, $aLines[$i])
            EndIf
        Next
    EndIf
EndFunc

 

Edited by Luke94
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...