GoogleDude Posted March 8, 2024 Posted March 8, 2024 So I have a GUI that creates a listbox with some items in it. when the gui starts it reads a list of items to select from a sqlite db and stores it in an array ($sItemsToSelect). At least it should. The issue is that its not selecting any items and im not sure why. The below example I believe demonstrates this. When the example gui below starts I am expecting it to select Items 1 and Items 3. Can someone help me correct the below please? expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiListBox.au3> Global $hGUI = GUICreate("Test GUI", 175, 120) Global $idList = GUICtrlCreateList("", 10, 10, 150, 100) GUICtrlSetData($idList, "Item 1|Item 2|Item 3|Item 4") GUISetState(@SW_SHOW, $hGUI) Global $aItemsToSelect = StringSplit("Item 1|Item 3", "|", 2) _SelectItems($idList, $aItemsToSelect) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func _SelectItems($idList, $aItemsToSelect) Local $hList = GUICtrlGetHandle($idList) ConsoleWrite("Selecting specified items..." & @CRLF) Local $iFound, $sItem, $iErrCount = 0 _GUICtrlListBox_SetSel($hList, False, -1) For $i = 0 To UBound($aItemsToSelect) - 1 $sItem = $aItemsToSelect[$i] $iFound = _GUICtrlListBox_FindString($hList, $sItem) If $iFound = -1 Then ConsoleWriteError("Item '" & $sItem & "' not found in ListBox." & @CRLF) $iErrCount += 1 Else _GUICtrlListBox_SetSel($hList, True, $iFound) ConsoleWrite("Selected item: '" & $sItem & "' at index " & $iFound & @CRLF) EndIf Next If $iErrCount > 0 Then MsgBox($MB_ICONERROR, "Error", "One or more items could not be selected. Check the console for details.") Else ConsoleWrite("All items selected successfully." & @CRLF) EndIf EndFunc Many Thanks in Advance!
Solution Musashi Posted March 8, 2024 Solution Posted March 8, 2024 (edited) Try this : expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiListBox.au3> Global $hGUI = GUICreate("Test GUI", 175, 120) Global $idList = GUICtrlCreateList("", 10, 10, 150, 100, BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL)) GUICtrlSetData($idList, "Item 1|Item 2|Item 3|Item 4") GUISetState(@SW_SHOW, $hGUI) Global $aItemsToSelect = StringSplit("Item 1|Item 3", "|", 2) _SelectItems($idList, $aItemsToSelect) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func _SelectItems($idList, $aItemsToSelect) Local $hList = GUICtrlGetHandle($idList) ConsoleWrite("Selecting specified items..." & @CRLF) Local $iFound, $sItem, $iErrCount = 0 For $i = 0 To UBound($aItemsToSelect) - 1 $sItem = $aItemsToSelect[$i] $iFound = _GUICtrlListBox_FindString($hList, $sItem) If $iFound = -1 Then ConsoleWriteError("Item '" & $sItem & "' not found in ListBox." & @CRLF) $iErrCount += 1 Else _GUICtrlListBox_SetSel($hList, $iFound) ConsoleWrite("Selected item: '" & $sItem & "' at index " & $iFound & @CRLF) EndIf Next If $iErrCount > 0 Then MsgBox($MB_ICONERROR, "Error", "One or more items could not be selected. Check the console for details.") Else ConsoleWrite("All items selected successfully." & @CRLF) EndIf EndFunc Edited March 8, 2024 by Musashi "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
GoogleDude Posted March 8, 2024 Author Posted March 8, 2024 That worked, Thank you very much. I was using ChatGPT but no matter how I worded it or what I gave it it just couldn't figure it out. Thanks again! Musashi 1
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