Jump to content

Recommended Posts

Posted

For those who use ListBoxes and wish to call a Function to grab the contents of the ListBox into a 1D array, then _GUICtrlListBox_CreateArray() is for you. It will Return an array with the contents of the ListBox inc. the number of rows. Simply run the Example to get an idea of the output. Thanks.

Function:

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListBox_CreateArray
; Description ...: Creates a 1-dimensional array from a listbox.
; Syntax ........: _GUICtrlListBox_CreateArray($hListBox)
; Parameters ....: $hListBox            - Control ID/Handle to the control
; Return values .: Success - The array returned is one-dimensional and is made up of the following:
;                                $aArray[0] = Number of rows
;                                $aArray[1] = 1st row
;                                $aArray[2] = 2nd row
;                                $aArray[n] = nth row
; Author ........: guinness
; Remarks .......: GUICtrlListBox.au3 should be included.
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListBox_CreateArray($hListBox)
    Local $iItemCount = _GUICtrlListBox_GetCount($hListBox)
    Local $aReturn[$iItemCount + 1] = [$iItemCount]
    For $i = 0 To $iItemCount - 1
        $aReturn[$i + 1] = _GUICtrlListBox_GetText($hListBox, $i)
    Next
    Return SetError(Number($aReturn[0] = 0), 0, $aReturn)
EndFunc   ;==>_GUICtrlListBox_CreateArray

Example use of Function:

#include <Array.au3> ; Required only for _ArrayDisplay().
#include <GUIListBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $iWidth = 600, $iHeight = 400, $iListBox = 0
    Local $hGUI = GUICreate('_GUICtrlListBox_CreateArray()', $iWidth, $iHeight, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))

    _CreateListBox($hGUI, $iListBox)

    Local $iGetArray = GUICtrlCreateButton('Get Array', $iWidth - 90, $iHeight - 28, 85, 25)
    GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)

    Local $iRefresh = GUICtrlCreateButton('Refresh', $iWidth - 180, $iHeight - 28, 85, 25)
    GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM)

    GUISetState(@SW_SHOW, $hGUI)

    Local $aReturn = 0
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $iGetArray
                $aReturn = _GUICtrlListBox_CreateArray($iListBox)
                _ArrayDisplay($aReturn, '_GUICtrlListBox_CreateArray() array.')

            Case $iRefresh
                GUICtrlDelete($iListBox)
                _CreateListBox($hGUI, $iListBox)

        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _CreateListBox($hGUI, ByRef $iListBox)
    Local $aClientSize = WinGetClientSize($hGUI)
    $iListBox = GUICtrlCreateList('', 0, 0, $aClientSize[0], $aClientSize[1] - 30)
    GUICtrlSetResizing($iListBox, $GUI_DOCKBORDERS)
    Sleep(250)
    __ListBoxFill($iListBox, Random(25, 100, 1)) ; Fill the ListBox with Random data.
EndFunc   ;==>_CreateListBox

Func __ListBoxFill($hListBox, $iRows) ; Required only for the Example.
    If Not IsHWnd($hListBox) Then
        $hListBox = GUICtrlGetHandle($hListBox)
    EndIf

    _GUICtrlListBox_BeginUpdate($hListBox)
    For $i = 0 To $iRows - 1
        _GUICtrlListBox_AddString($hListBox, 'Row ' & $i + 1)
    Next
    _GUICtrlListBox_EndUpdate($hListBox)
EndFunc   ;==>__ListBoxFill

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Good stuff man! Thank you. I may use this in a near future. :)

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

  On 11/21/2012 at 10:32 PM, 'careca said:

Good stuff man! Thank you. I may use this in a near future. :)

You're welcome.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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...