Jump to content

Selecting Multiple ListBox items from an array?


Go to solution Solved by Musashi,

Recommended Posts

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?

#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!

Link to comment
Share on other sites

  • Solution
Posted (edited)

Try this :

#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 by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
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
 Share

×
×
  • Create New...