Jump to content

Looping an array to ListView


Tomoya
 Share

Recommended Posts

I have a file that I am trying to add to a list view that has multiple columns. I am having a problem getting it to add the correct items to the correct subitem's column. I think I almost have it but I'm sure I am missing some small thing.

The files looks as such.

1;2;3;4;5;6

Here is the function.

Func _FillList()
    $array = 1
    $data = 1
    Local $file = @ScriptDir & "" & "mob.txt"
    $data = FileRead($file)
    $array = StringSplit($data, ";", 1)
    For $n = 1 To UBound($array) - 1
        If Mod($n, 2) Then
            $subitem = _GUICtrlListView_AddItem($ListView, $array[$n])
        Else
            _GUICtrlListView_AddSubItem($ListView, $subitem, $array[$n], 1)
            $a = UBound($array) - 1
            If $n + 1 > $a Then
                ExitLoop
            Else
                _GUICtrlListView_AddSubItem($ListView, $subitem, $array[$n + 1], 2)
            EndIf
        EndIf
    Next
EndFunc ;===>_FillList

Is there a simple way to just tell it to move to the next item in the loop? Currently the way it is displaying is

1 | 2 | 3
3 | 4 | 5
5 | 6
Edited by Tomoya
Link to comment
Share on other sites

  • Moderators

Tomoya,

And what do you want it to look like? :huh:

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

  • Moderators

Tomoya,

I would Step through the array in the required number of columns - like this:

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

$hGUI = GUICreate("Test", 500, 500)

$cListView = GUICtrlCreateListView("", 10, 10, 310, 200)
_GUICtrlListView_AddColumn($cListView, "Col 1", 100)
_GUICtrlListView_AddColumn($cListView, "Col 2", 100)
_GUICtrlListView_AddColumn($cListView, "Col 3", 100)

GUISetState()

; Simulate loading file
$sData = "1;2;3;4;5;6"
$aData = StringSplit($sData, ";")

For $i = 1 To $aData[0] Step 3
    $iItem = _GUICtrlListView_AddItem($cListView, $aData[$i])
    _GUICtrlListView_AddSubItem($cListView, $iItem, $aData[$i + 1], 1)
    _GUICtrlListView_AddSubItem($cListView, $iItem, $aData[$i + 2], 2)
Next


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Much simpler. ;)

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

Tomoya,

I would Step through the array in the required number of columns - like this:

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

$hGUI = GUICreate("Test", 500, 500)

$cListView = GUICtrlCreateListView("", 10, 10, 310, 200)
_GUICtrlListView_AddColumn($cListView, "Col 1", 100)
_GUICtrlListView_AddColumn($cListView, "Col 2", 100)
_GUICtrlListView_AddColumn($cListView, "Col 3", 100)

GUISetState()

; Simulate loading file
$sData = "1;2;3;4;5;6"
$aData = StringSplit($sData, ";")

For $i = 1 To $aData[0] Step 3
$iItem = _GUICtrlListView_AddItem($cListView, $aData[$i])
_GUICtrlListView_AddSubItem($cListView, $iItem, $aData[$i + 1], 1)
_GUICtrlListView_AddSubItem($cListView, $iItem, $aData[$i + 2], 2)
Next


While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd

Much simpler. ;)

M23

That worked perfectly... I had no idea about step and for what I was trying to accomplish it is much more efficient. I have a personal question for you if you don't mind. What is your background in AutoIt? I see that you answer almost every question with a elegant and simple solution posted here and that is awesome! Do you currently have any AutoIt challenges that "Stump" you right now?

Thanks for helping the community.

Link to comment
Share on other sites

  • Moderators

Tomoya,

What is your background in AutoIt?

I discovered AutoIt shortly before I joined the forum - but I have been coding as a hobby for some 45 years (mostly FORTRAN, BASIC (of several dialects), ASM, and a few others). ;)

any AutoIt challenges that "Stump" you right now?

That would be telling! :P

Thanks for helping the community

It is my pleasure to be here. :>

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

Tomoya,

(bowing to M23...)

This may interest you. This uses some of the techniques used in _arraydisplay with automatic column adjustment (code from M23). I find this simpler than using the listview UDF's, although my experience with listview controls is limited.

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

local $col_N    = 50    ; number of columns in listview
local $item_N   = 128   ; number of test entries to generate

local $col_W    = @DesktopWidth/$col_N
local $gui010   = GUICreate("Test LV", ($col_W * $col_N) - 10, 500)
local $clsize   = wingetclientsize($gui010)
local $lst010   = GUICtrlCreateListView('', 0, 0, $clsize[0], $clsize[1] - 100,-1,bitor($LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT))
for $1 = 1 to $col_N
    _GUICtrlListView_AddColumn($lst010, 'Col ' & stringformat('%02i',$1), $col_W)
Next

; generate a bunch of delimited test data.  The '|' is the column delimiter, the ';' is the item delimiter

local $sdata
for $1 = 1 to $item_N
    $sdata &= $1 & '|'
    if mod($1,$col_N) = 0 then $sdata &= ';'
next

local $adata = stringsplit($sdata,';',2)

For $i = 0 To ubound($aData) - 1
    GUICtrlCreateListViewItem($aData[$i], $lst010)
Next

; adjust column widths to the wider of column header or column data
;
; This magic compliments of Melba23

local $ghw, $gdw, $gtw
for $1 = 0 to _GUICtrlListView_GetColumnCount($lst010) + 1
    _GUICtrlListView_SetColumnWidth($lst010, $1, $LVSCW_AUTOSIZE_USEHEADER)
    $ghw = _GUICtrlListView_GetColumnWidth($lst010, $1)
    _GUICtrlListView_Setcolumnwidth($lst010, $1, $LVSCW_AUTOSIZE)
    $gdw = _GUICtrlListView_GetColumnWidth($lst010, $1)
    if $ghw > $gdw then
        _GUICtrlListView_Setcolumnwidth($lst010, $1, $ghw)
        $gtw += $ghw
    Else
        $gtw += $gdw
    endif
next

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

kylomas

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

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