Jump to content

Get list box items

Recommended Posts

How do I get strings from a list box?

I am able to add strings with this:

ControlCommand ("MDSI", "", 1, "AddString", "string")

I am not able to use controlgettext.



To use ControlCommand(), you would select each item in a loop with "SetCurrentSelection", and get its text with "GetCurrentSelection". The loop exits when selection fails:
#include <GuiConstantsEx.au3>

Global $hGUI, $ctrlButton, $ctrlLB
$hGUI = GUICreate("MDSI", 300, 300)
$ctrlLB = GUICtrlCreateList("Item 0", 20, 20, 260, 210)
For $n = 1 To 19
    GUICtrlSetData($ctrlLB, "Item " & $n)
$ctrlButton = GUICtrlCreateButton("READ", 100, 250, 100, 30)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
        Case $ctrlButton

Func _ReadItems()
    Local $n = 0, $sTxt = "", $sMsg = ""
    While 1
        ControlCommand("MDSI", "", $ctrlLB, "SetCurrentSelection", $n)
        If @error Then ExitLoop ; Done
        $sTxt = ControlCommand("MDSI", "", $ctrlLB, "GetCurrentSelection", "")
        $sMsg &= $n & ":  " & $sTxt & @CRLF
        $n += 1
    MsgBox(64, "Result", $sMsg)
EndFunc   ;==>_ReadItems

There are more advanced UDF functions that can get the count and read the items without selecting each one, like _GUICtrlListBox_GetCount() and _GUICtrlListBox_GetText():

#include <GuiListBox.au3>

; Rest of script body is the same...

Func _ReadItems()
    Local $hLB = ControlGetHandle("MDSI", "", $ctrlLB)
    Local $iCnt = _GUICtrlListBox_GetCount($hLB)
    Local $sMsg = ""
    For $n = 0 To $iCnt - 1
        $sMsg &= $n & ":  " & _GUICtrlListBox_GetText($hLB, $n) & @CRLF
    MsgBox(64, "Result", $sMsg)
EndFunc   ;==>_ReadItems


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...