Jump to content

How to list array in list box


antmar904
 Share

Recommended Posts

hi all very new to autoit and scripting.

i am trying to get a list of users profiles on a remote pc then list them in a gui list box but i am having issues.

please don't laugh at my code if it is totally wrong, im trying LOL :)

thanks in advance.

#Include <Array.au3>
#Include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


Global $profileList, $buttonGet, $name

#Region ### START Koda GUI section ### Form=
$mainForm = GUICreate("List Profiles", 413, 454, 416, 240)
GUISetBkColor(0xC0DCC0)
$labelComputerName = GUICtrlCreateLabel("Computer Name:", 16, 40, 83, 17)
$inputComputerName = GUICtrlCreateInput("Please enter computer name", 108, 32, 201, 21)
$listProfiles = GUICtrlCreateList("", 109, 96, 201, 188)
$bottonGet = GUICtrlCreateButton("Get", 320, 32, 51, 25, $WS_GROUP)
$bottonDelete = GUICtrlCreateButton("Delete All", 232, 288, 75, 25, $WS_GROUP)
$bottonExit = GUICtrlCreateButton("Exit", 296, 376, 75, 25, $WS_GROUP)
$labelVersion = GUICtrlCreateLabel("List Profiles v1.0", 16, 416, 81, 17)
$labelComputerName2 = GUICtrlCreateLabel("", 154, 80, 4, 4)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


$nMsg = GUIGetMsg()
Switch $nMsg
Case $buttonGet
   $name = GUICtrlRead($inputComputerName)
   $profileList = _FileListToArray("\\" & $name & "\C$\Documents and Settings\" , "*" , 2)
   _ArrayDisplay($profileList)
   GUICtrlSetData($listProfiles, $profileList)
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Link to comment
Share on other sites

antmar904,

You need to load your listbox in a loop, currently you are setting it equal to the array. The following is an example of this

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

#AutoIt3Wrapper_Add_Constants=n

#include <array.au3>
#include <string.au3>

Example2()

func Example2()

    ; Style $WS_Vscroll overrides the default styles which include $LBS_SORT. $LBS_SORT prevents blank columns from being displayed in the correct order

    local $gui010   =   guicreate('Listbox Example',900)                        ; set width to 900 pixels
    local $lbx010   =   guictrlcreatelist('',10,10,800,380,$ws_vscroll)
    local $btn010   =   guictrlcreatebutton('Populate',813,10,80,25)
    local $btn020   =   guictrlcreatebutton('Clear',813,45,80,25)
                        guictrlsetfont($btn010,8.5,600)
                        guictrlsetfont($btn020,8.5,600)
                        guictrlsetcolor($btn010,0x00aa00)
                        guictrlsetcolor($btn020,0xaa0000)
                        guictrlsetfont($lbx010,8.5,600,default,'courier new')   ; use mono spaced font for consistent spacing
                        guisetstate()

    ; 2D array of data to load to the list control in formatted columns

    local $aLBstr[10][4] = [ _
                        ['column1','col1 data','I am in column two','shoes, ships and candlewax'], _
                        ['',15,'abcdefghijkl','col3'], _
                        ['','Dodge','Ram','1500'], _
                        ['','Alexandra','','HotBody'], _
                        ['','','','only one column?'], _
                        ['something...','John','Q.','Smith'] _
                            ]
    local $msg
    while 1
        $msg = guigetmsg()
        switch $msg
            Case $gui_event_close
                Exit
            case $btn010
                local $str
                for $i = 0 to ubound($aLBstr,1) - 1
                    for $k = 0 to ubound($aLBstr,2) - 1
                        $str &= $aLBstr[$i][$k] & _stringrepeat(' ',20-stringlen($aLBstr[$i][$k]))
                    Next
                    guictrlsetdata($lbx010,$str)
                    $str = ''
                next
            case $btn020
                guictrlsetdata($lbx010,'')
        endswitch
    wend

endfunc

kylomas

edit: just noticed that this example uses a 2D array, you can easily adapt it to a 1D array, however

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Kylomas,

i don't understand what you mean by "You need to load your listbox in a loop, currently you are setting it equal to the array".

i don't understand your example either, i tried to but im not getting it.

here is the output of the array that i would like to list in the list box:

image link: http://sdrv.ms/YMUGRt

i would like to list col 0 row 1 - 0 but i cant seem to get it.

here is what i have so far, i can only seem figure out how to list one of the rows.

#Include <Array.au3>
#Include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $profileList, $buttonGet, $name
#Region ### START Koda GUI section ### Form=
$mainForm = GUICreate("List Profiles", 413, 454, 416, 240)
GUISetBkColor(0xC0DCC0)
$labelComputerName = GUICtrlCreateLabel("Computer Name:", 16, 40, 83, 17)
$inputComputerName = GUICtrlCreateInput("Enter Computer Name Here", 108, 32, 201, 21)
$listProfiles = GUICtrlCreateList("", 109, 96, 201, 188)
$buttonGet = GUICtrlCreateButton("Get", 320, 32, 51, 25, $WS_GROUP)
$buttonDelete = GUICtrlCreateButton("Delete All", 232, 288, 75, 25, $WS_GROUP)
$buttonExit = GUICtrlCreateButton("Exit", 296, 376, 75, 25, $WS_GROUP)
$labelVersion = GUICtrlCreateLabel("List Profiles v1.0", 16, 416, 81, 17)
$labelComputerName2 = GUICtrlCreateLabel("", 154, 80, 4, 4)
GUISetState()
#EndRegion ### END Koda GUI section ###
   While 1
   $nMsg = GUIGetMsg()
   Select
   Case $nMsg = $buttonGet
   $name = GUICtrlRead($inputComputerName)
   $profileList = _FileListToArray("" & $name & "C$Documents and Settings" , "*" , 2)
   GUICtrlSetData($listProfiles, $profileList[2])
   Case $nMsg = $buttonExit
   Exit
   Case $nMsg = $GUI_EVENT_CLOSE
   Exit
   EndSelect
   WEnd

thank you again.

Edited by antmar904
Link to comment
Share on other sites

antmar904,

Here is the mofified code to list all entries returned from _filelisttoarray. This is modified to run on my machine. The significant part is looping through the array to populate the listbox. If you are not familiar with arrays, there are excellent references in both the help file and on Wiki. Just search on "autoit array".

#include <Array.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $profileList, $buttonGet, $name
#region ### START Koda GUI section ### Form=
$mainForm = GUICreate("List Profiles", 413, 454, 416, 240)
GUISetBkColor(0xC0DCC0)
$labelComputerName = GUICtrlCreateLabel("Computer Name:", 16, 40, 83, 17)
$inputComputerName = GUICtrlCreateInput("Enter Computer Name Here", 108, 32, 201, 21)
$listProfiles = GUICtrlCreateList("", 109, 96, 201, 188)
$buttonGet = GUICtrlCreateButton("Get", 320, 32, 51, 25, $WS_GROUP)
$buttonDelete = GUICtrlCreateButton("Delete All", 232, 288, 75, 25, $WS_GROUP)
$buttonExit = GUICtrlCreateButton("Exit", 296, 376, 75, 25, $WS_GROUP)
$labelVersion = GUICtrlCreateLabel("List Profiles v1.0", 16, 416, 81, 17)
$labelComputerName2 = GUICtrlCreateLabel("", 154, 80, 4, 4)
GUISetState()
#endregion ### END Koda GUI section ###
While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $buttonGet
            $name = GUICtrlRead($inputComputerName)
            $profileList = _FileListToArray("C:Users", "*", 2)

            ; This is how it would work on my machine (win7 home)

            ;GUICtrlSetData($listProfiles, $profileList[2])            ; this lists array element # 3 only
            ; This willpopulate thelistbox with all directories (users) returned to the array)
            for $i = 1 to ubound($profilelist) - 1
                guictrlsetdata($listProfiles,$profilelist[$i])
            next
        Case $nMsg = $buttonExit
            Exit
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

Good Luck,

kylomas

edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

thank you kylomas that worked great.

one more question.

is it possible to create a label that shows Row 0 and its Colume 0 from the $profileList Array?

here is what i have so far:

$labelTotal = GUICtrlCreateLabel("Total Profiles:" & $profileList [0], 109, 300, 90, 25)
Edited by antmar904
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

  • Recently Browsing   0 members

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