Jump to content

Listbox and arrays?


Sjov2200
 Share

Recommended Posts

Hi,

I am trying to make a listbox (I tried with Koda, but failed miserably) which displays content of an array, with the possibility to change the data directly.

Like:

a,1, , ,comment about line 1

a,0,1200, ,comment about line 2

b,10, ,comment about line 3

So my array should hold (one letter, a number, a number, text max 254) for x number of lines.

I am finding the coupling between the GUI controls and the array difficult.

My GUI should look something like this

Letter__#_____number____Comment

------------------------------------------------------------

A______1______0________bla bla bla

B______2______10_______more bla bla

F______12_____0________even more bla

Y______3______1200_____more bla bla

G______1______0________bla bla

- - - - <-- new lines to array here

Edited by Sjov2200
Link to comment
Share on other sites

  • Moderators

Sjov2200,

First, welcome to the AutoIt forums. :)

When you post here it always helps if you include the code you have been working on to try and solve your problem. Having some code to work on is a great help - and no-one here is too keen to help the "code it for me" brigade.

Reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here. There are even video tutorials on YouTube if you prefer watching to reading.

But as it can be difficult to get started, take a look at this and see if it is close to your needs: ;)

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

; Create initial data array
Global $aArray[5][4] = [["A", 1, 0, "bla bla bla"], ["B", 2, 10, "more bla bla"], ["F", 12, 0, "even more bla"], _
                        ["Y", 3, 1200, "more bla bla"], ["G", 1, 0, "bla bla"]]

Global $hEdit[4]

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

; Create edits
$hEdit[0] = GUICtrlCreateEdit("",  10, 30,  30, 20, $WS_TABSTOP)
$hEdit[1] = GUICtrlCreateEdit("",  40, 30,  30, 20, $WS_TABSTOP)
$hEdit[2] = GUICtrlCreateEdit("",  70, 30,  70, 20, $WS_TABSTOP)
$hEdit[3] = GUICtrlCreateEdit("", 140, 30, 350, 20, $WS_TABSTOP)

; Create buttons
$hAmend_Button = GUICtrlCreateButton("Amend", 10, 60, 80, 30)
$hAdd_Button = GUICtrlCreateButton("Add", 100, 60, 80, 30)
$hDelete_Button = GUICtrlCreateButton("Delete", 190, 60, 80, 30)

; Create listview
$hListView = GUICtrlCreateListView("Letter|#|Number|Comment", 10, 100, 480, 380)
; Set col width
_GUICtrlListView_SetColumnWidth(-1, 0, 30)
_GUICtrlListView_SetColumnWidth(-1, 1, 30)
_GUICtrlListView_SetColumnWidth(-1, 2, 70)
_GUICtrlListView_SetColumnWidth(-1, 3, 350)
; Add data to array
_GUICtrlListView_AddArray($hListView, $aArray)

GUISetState()

; Set selection variable
$iIndex = ""
$iCurr_Index= ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hAmend_Button
            ; Check we have a selection
            If $iIndex <> "" Then
                ; Change the data in the array
                For $i = 0 To 3
                    $aArray[$iIndex][$i] =  GUICtrlRead($hEdit[$i])
                Next
                ; Reset the listview data
                _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
                _GUICtrlListView_AddArray($hListView, $aArray)
            EndIf
        Case $hAdd_Button
            ; Add to size of array
            $iNewDimension = UBound($aArray) + 1
            ReDim $aArray[$iNewDimension][4]
            ; Add new data to array
            For $i = 0 To 3
                $aArray[$iNewDimension - 1][$i] =   GUICtrlRead($hEdit[$i])
            Next
            ; Reset the listview data
            _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
            _GUICtrlListView_AddArray($hListView, $aArray)
        Case $hDelete_Button
            ; Check we have a selection
            If $iIndex <> "" Then
                ; Move up other rows
                For $j = $iIndex To UBound($aArray) - 2
                    For $i = 0 To 3
                        $aArray[$j][$i] = $aArray[$j + 1][$i]
                    Next
                Next
                ; Reduce array size
                ReDim $aArray[UBound($aArray) - 1   ][4]
                ; Reset the listview data
                _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
                _GUICtrlListView_AddArray($hListView, $aArray)
            EndIf
    EndSwitch

    ; Determine current selected item and transfer data to the edits
    $iIndex = _GUICtrlListView_GetSelectedIndices($hListView, False)
    ; Check it is a new index to prevent flicker
    If $iIndex <> $iCurr_Index Then
        For $i = 0 To 3
            GUICtrlSetData($hEdit[$i], $aArray[$iIndex][$i])
        Next
        $iCurr_Index = $iIndex
    EndIf

WEnd

I think the comments are clear enough, but do ask if there is anything you do not understand.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sjov2200,

First, welcome to the AutoIt forums. :)

When you post here it always helps if you include the code you have been working on to try and solve your problem. Having some code to work on is a great help - and no-one here is too keen to help the "code it for me" brigade.

Reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here. There are even video tutorials on YouTube if you prefer watching to reading.

But as it can be difficult to get started, take a look at this and see if it is close to your needs: ;)

I think the comments are clear enough, but do ask if there is anything you do not understand.

M23

Hey! Thx man... I will try it out as soon as I get home :-).

Ye' sorry for not posting the things that I've tried, but I was'nt really anywhere near a solution, so I think posting would have confused matters more then benefitted ^^.

I did try out some examples under help, but I found it hard to grasp the function of the different Gui ctrl's ...

Edited by Sjov2200
Link to comment
Share on other sites

Hey! Thx man... I will try it out as soon as I get home :-).

Ye' sorry for not posting the things that I've tried, but I was'nt really anywhere near a solution, so I think posting would have confused matters more then benefitted ^^.

I did try out some examples under help, but I found it hard to grasp the function of the different Gui ctrl's ...

Oh man, that's fantastic. Was exactly what I was looking for... I will tweak it to fit my little proggie :)

THANKS!!!

Link to comment
Share on other sites

  • Moderators

Sjov2200,

Delighted I could help. Posts like that really brighten up a cold autumn day. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sjov2200,

Delighted I could help. Posts like that really brighten up a cold autumn day. ;)

M23

So, my proggie works fine, listbox updates are working, I have added a column to the array and the listbox no problem there.

My next development is saving / loading the arrays and I found an earlier post using the Save_Array command, I will be looking into that.

I was trying my own version with loops, comma seperation and fileread and it works fine. Only sometimes Filegetsize returns the wrong size of the file?!? It happens when I load once and then I load the same file. Filegetsize returns the wrong size and my loop is out of bounds of course...

:)

Edited by Sjov2200
Link to comment
Share on other sites

  • Moderators

Sjov2200,

It is obvious that your problem is line 5628. ;)

Seriously, if you do not post the code, how are we expected to offer suggestions on what might be wrong. My crystal ball is having an off-day! B)

However, the best way to save/load arrays into/from files is to use the _FileReadToArray and _FileWriteFromArray functions. They are easy to use and do all the work for you! Just look them up in the Help file - then you can forget about FileGetSize etc. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sjov2200,

It is obvious that your problem is line 5628. ;)

Seriously, if you do not post the code, how are we expected to offer suggestions on what might be wrong. My crystal ball is having an off-day! B)

However, the best way to save/load arrays into/from files is to use the _FileReadToArray and _FileWriteFromArray functions. They are easy to use and do all the work for you! Just look them up in the Help file - then you can forget about FileGetSize etc. :)

M23

Nah :-) I like to get pointers and figuring out the rest myself.

So, I have succes with _FileReadToArray & _FileWriteFromArray makes my code much cleaner too (not so many variables needed anymore).

Thx again :-)

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