Jump to content

Arrays - Search help?


algiuxas
 Share

Recommended Posts

Hello,

I need help with arrays, I have array with clients( ID, NAME, etc.), and I need to show their IDs and names on list:

#include <Array.au3>
$Clients[5,5] = _
[[ "1560", "Ben",           "Age: 25", "Bla bla...", "Bla bla..."], _
 [ "1025", "Somebody",      "Age: 14", "Bla bla...", "Bla bla..."], _
 [ "9684", "John",          "Age: 36", "Bla bla...", "Bla bla..."], _
 [ "1002", "Laura",         "Age: 15", "Bla bla...", "Bla bla..."], _
 [ "1234", "Another guy",   "Age: 18", "Bla bla...", "Bla bla..."]]
;  ID      NAME             AGE        BLA BLA BLA   BLA BLA BLA

_ArrayDisplay($Clients)

;Create simple GUI
$Form1 = GUICreate("Select client:", 50, 200)

;Create list of clients
$List_Clients = GUICtrlCreateList("", 0, 0, 50, 200)
GUISetState(@SW_SHOW)

;How to make it only show IDs and names? ("1560: Ben"|"1025: Somebody"|...)
GUICtrlSetData($List_Clients,"???")

; While...WEnd

$Selected = _GUICtrlListView_GetSelectedIndices($List_Clients);Get selected ID and name

;How to make it find out what client is selected? (row of array)

And then when you select ID & name it would find row of array $Clients.

Thanks :)

 

After switching years ago to Linux, sadly I don't use AutoIt anymore.

Link to comment
Share on other sites

algiuxas,

This should get you started, although for multi-column list I always use a ListView control...

#include <GUIConstantsEx.au3>
#include <Array.au3>

local $Clients = _
[[ "1560", "Ben",           "Age: 25", "Bla bla...", "Bla bla..."], _
 [ "1025", "Somebody",      "Age: 14", "Bla bla...", "Bla bla..."], _
 [ "9684", "John",          "Age: 36", "Bla bla...", "Bla bla..."], _
 [ "1002", "Laura",         "Age: 15", "Bla bla...", "Bla bla..."], _
 [ "1234", "Another guy",   "Age: 18", "Bla bla...", "Bla bla..."]]
;  ID      NAME             AGE        BLA BLA BLA   BLA BLA BLA

;_ArrayDisplay($Clients)

;Create simple GUI
$Form1 = GUICreate("Select client:", 150, 200)

;Create list of clients
$List_Clients = GUICtrlCreateList("", 10, 10, 130, 200)
GUISetState(@SW_SHOW)

; populate list with ID and NAME

for $i = 0 to ubound($Clients) - 1
    guictrlsetdata($List_Clients,$Clients[$i][0] & ' - ' & $Clients[$i][1])
next

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
        case $List_Clients
            _get_client_detail(guictrlread($List_Clients))
    EndSwitch

WEnd



func _get_client_detail($str)

    local $cNum = stringsplit($str,' - ',3)[0], $out = ''
    for $i = 0 to ubound($Clients) - 1
        if $Clients[$i][0] = $cNum then
            for $j = 0 to ubound($Clients,2) - 1
                $out &= $Clients[$i][$j] & ','
            Next
        EndIf

    Next

    msgbox(0,'CLIENT DETAIL', $out)

endfunc

kylomas

edit: A listview example...

#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <GuiListView.au3>

Local $Clients = _
        [["1560", "Ben", "Age: 25", "Bla bla...", "Bla bla..."], _
        ["1025", "Somebody", "Age: 14", "Bla bla...", "Bla bla..."], _
        ["9684", "John", "Age: 36", "Bla bla...", "Bla bla..."], _
        ["1002", "Laura", "Age: 15", "Bla bla...", "Bla bla..."], _
        ["1234", "Another guy", "Age: 18", "Bla bla...", "Bla bla..."]]
;  ID      NAME             AGE        BLA BLA BLA   BLA BLA BLA

;_ArrayDisplay($Clients)

;Create simple GUI
$Form1 = GUICreate("Select client:", 150, 200)

;Create list of clients
$List_Clients = GUICtrlCreateListView("ID|Name", 10, 10, 130, 200)

; dummy control actioned by notify routine when user clicks on control
$dummy_detail = GUICtrlCreateDummy()

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; populate listview columns with ID and NAME
For $i = 0 To UBound($Clients) - 1
    _GUICtrlListView_AddItem($List_Clients, $Clients[$i][0])
    _GUICtrlListView_AddSubItem($List_Clients, $i, $Clients[$i][1], 1)
Next

; distribute column widths evenly to show all data
For $i = 0 To _GUICtrlListView_GetColumnCount($List_Clients)
    _GUICtrlListView_SetColumnWidth($List_Clients, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
            ; this control is actioned by the notification routine
        Case $dummy_detail
            _get_client_detail(_GUICtrlListView_GetItemTextString($List_Clients, -1))
    EndSwitch



WEnd



Func _get_client_detail($str)

    $str = StringRegExpReplace($str, '([^\|])\|.*', '$1')

    Local $out
    For $i = 0 To UBound($Clients) - 1
        If $Clients[$i][0] <> $str Then ContinueLoop

        For $j = 0 To UBound($Clients, 2) - 1
            $out &= $Clients[$i][$j] & ' '
        Next
    Next
    MsgBox(0, 'CLIENT DETAIL', $out)

EndFunc   ;==>_get_client_detail

; routine to run when user clicks on any entry in the listview

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    Switch $tNMHDR.IDFrom

        Case $List_Clients
            Switch $tNMHDR.Code
                Case $nm_click
                    GUICtrlSendToDummy($dummy_detail) ; action dummy control in the message loop
            EndSwitch

    EndSwitch



    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Edited by kylomas
listview example

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

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