Jump to content
Sign in to follow this  

Adding an Array to a ListView control

Recommended Posts


How do you insert an function generated array into a ListView control? And by "function generated", I mean that a function runs, gets some data, and then if it the function succeeds, it returns an array. I want to place that generated array in a ListView.

In one of my previous threads, enaiman told me that there was a function which would add an array to a listview control (_GUICtrlListView_AddArray), so I tried using it like this:

$LeftBorder = 10

$ListView = GUICtrlCreateListView ("", $LeftBorder, 75, 390, 425)
_GUICtrlListView_AddColumn ($ListView, "Col 1")
_GUICtrlListView_SetItemCount ($ListView, 750)
_GUICtrlListView_AddArray ($ListView, $Array)

and it failed miserably (incorrect number of subscripts), which I predicted would happen... Apparently, you need to declare each element in the array (do you have to?) first, so how would I do that with the function-generated array? Even if I try $Array[170][1] (there's 170 elements, and one column), it doesn't work too well.


$LeftBorder = 10

Global $Array[170][1]

$ListView = GUICtrlCreateListView ("", $LeftBorder, 75, 390, 425)
_GUICtrlListView_AddColumn ($ListView, "Col 1")
_GUICtrlListView_SetItemCount ($ListView, 750)
_GUICtrlListView_AddArray ($ListView, $Array)

This code (right above this sentence) doesn't give me an error, but returns nothing in the list view when I load it.

_GUICtrlListView_AddArray ($ListView, $Array[170][1])

And when I replace the last line with this, it still returns nothing.


Am I doing something wrong? Thanks for your help! =)


random notes:

Here's the function for the array, based on Danny35d's in a previous thread:

Func _ProgramList ()
    Local $Count = 1
    While 1 
        $EnumKey = RegEnumKey ($ProgramReg, $Count)
        If Not @error = 0 then ExitLoop
        $ReadName = RegRead ($ProgramReg & "\" & $EnumKey, "DisplayName")
        $ReadName = StringReplace ($ReadName, " (remove only)", "")
        If Not $ReadName = "" Then
            ReDim $ProgramArray[UBound($ProgramArray) + 1]
            $ProgramArray[0] = UBound($ProgramArray) - 1
            $ProgramArray[UBound($ProgramArray) - 1] = $ReadName
        $Count = $Count + 1
    If IsDeclared("ProgramArray") = 1 Then
            Return ($ProgramArray)
    ElseIf IsDeclared("ProgramArray") = 0
        SetError (1)

And if this matters, I use AutoIt v. (stable).

Thanks for your help again! =)

Share this post

Link to post
Share on other sites

C'mon, the help file content regarding _GUICtrlListView_AddArray is very easy to understand and the example there have everything you need.

All I did is: I stripped all useless code and here is a working result - it shows that an array will be correctly added to the List View control:

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

Opt('MustDeclareVars', 1)

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work


Func _Main()
    Local $iI, $iTimer, $hListView

    ; Create GUI
    GUICreate("ListView Add Array", 400, 300)
    $hListView = GUICtrlCreateListView("", 2, 2, 394, 268)

    ; Add columns
    _GUICtrlListView_AddColumn($hListView, "Program", 100)

    ; this block of code only generates the array
    Local $aItems[200][1]
    For $iI = 0 To UBound($aItems) - 1
        $aItems[$iI][0] = "Item " & $iI

    _GUICtrlListView_AddArray($hListView, $aItems)

    ; Loop until user exits
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

Your $ProgramArray is uni-dimensional so the example is exactly as you need.

It works IF $ProgramArray is an array - if it is not, it won't work.

Did you think to check the content of $ProgramArray before attempting to put it in the ListView? A simple _ArrayDisplay can save you alot of headaches.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post

Link to post
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
Sign in to follow this