Jump to content

how to sum ​​rows from the listbox


Recommended Posts

  • Moderators

lastmember,

Welcome to the AutoIt forum. :)

Here is one way to do it: ;)

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>

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

$cList = GUICtrlCreateList("", 10, 10, 200, 200)
For $i = 1 To 5
    GUICtrlSetData($cList, Random(1, 100, 1))
Next

$cButton_Add = GUICtrlCreateButton("Add", 250, 10, 80, 30)

$cLabel = GUICtrlCreateLabel("", 10, 250, 200, 20)

$cButton_Sum = GUICtrlCreateButton("Sum", 10, 300, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_Add
            GUICtrlSetData($cList, Random(1, 100, 1))
        Case $cButton_Sum
            $iCount = _GUICtrlListBox_GetCount($cList)
            $iSum = 0
            For $i = 0 To $iCount - 1
                $iSum += Number(_GUICtrlListBox_GetText($cList, $i))
            Next
            GUICtrlSetData($cLabel, $iSum)
    EndSwitch

WEnd

All clear? Please ask if not :)

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

Now I have another question

I have the following:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("test", 288, 307, 202, 164)
$List1 = GUICtrlCreateList("", 5, 4, 273, 227)
$Button1 = GUICtrlCreateButton("test", 7, 236, 268, 33)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $Button1
read()
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd

Func read()                                         
$iniread = IniReadSection(@ScriptDir & "test.ini", "section1")
If @error Then                                                                  
MsgBox(4096, "", "no have ini file!")              
Else                                                                           
For $i = 1 To $iniread[0][0]
GUICtrlSetData($list1,$iniread[$i][1])
Next
endif
EndFunc

in test.ini section1, we next:

key1=test

key2=test

key3=test1

key4=test2

but I do not upload $ List1 than

test

test1

test2

where am I wrong to not load all 4 keys

not repeat those loading than once

thanks

Link to comment
Share on other sites

  • Moderators

lastmember,

Please start a new topic if you have a new question - we do not mind at all. :)

GUICtrlSetData works strangely for List controls. If you try to add something that is already present, it does not do so - which is why you only get the one "test". You need to add all the items at the same time like this: ;)

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

$Form1 = GUICreate("test", 288, 307, 202, 164)
$List1 = GUICtrlCreateList("", 5, 4, 273, 227)
$Button1 = GUICtrlCreateButton("test", 7, 236, 268, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            read()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func read()
    $iniread = IniReadSection(@ScriptDir & "test.ini", "section1")
    If @error Then
        MsgBox(4096, "", "no have ini file!")
    Else
        $sIniFill = ""
        For $i = 1 To $iniread[0][0]
            $sIniFill &= "|" & $iniread[$i][1]
        Next
        GUICtrlSetData($List1, $sIniFill)
    EndIf
EndFunc   ;==>read

Now you get any multiple entries added to the list. All clear? :)

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

lastmember,

As we need to fill the list control in a single command to make sure we get possible duplicate entries, we need to tell AutoIt when each entry begins and ends in the string we pass to the list - we do this using the GUIDataSeparatorChar. By default this is set to "|", but you can change it using Opt.

So $sIniFill &= "|" & $iniread[$i][1] just adds the separator between each entry - and the fact that there is a leading "|" means that any previous content is destroyed, as explained in the Help file entry for GUICtrlSetData.

All clear now? :)

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

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