mike2003 Posted June 17, 2021 Posted June 17, 2021 I made a GUICtrlCreateList and changed the content, and now I want to save it. Then restore via GUICtrlSetData. But how to save?
Nine Posted June 17, 2021 Posted June 17, 2021 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
mike2003 Posted June 17, 2021 Author Posted June 17, 2021 Local $sText='' For $i = 0 To _GUICtrlListBox_GetListBoxInfo($List1) - 1 $sText &= _GUICtrlListBox_GetText($List1, $i) & @CRLF Next I preferred to collect everything in one variable.
Luke94 Posted June 17, 2021 Posted June 17, 2021 expandcollapse popup#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 AlexanderB 1
Nine Posted June 17, 2021 Posted June 17, 2021 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) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Luke94 Posted June 17, 2021 Posted June 17, 2021 (edited) 20 minutes ago, Luke94 said: expandcollapse popup#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 June 17, 2021 by Luke94
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now