Jump to content
Sign in to follow this  
lastmember

how to sum ​​rows from the listbox

Recommended Posts

lastmember

hello

There are beginner and want to know how can amount to several numbers that are in a listbox

example:

2

5

3

How do I sum (10) in a label ?

thanks in advance

Edited by lastmember

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
lastmember

yeess!!

works well

thank you very much !

Share this post


Link to post
Share on other sites
lastmember

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

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
lastmember

yes it works ...

I need to document to understand what is written:

$sIniFill &= "|"

Thanks again for the prompt and you had patience with me!

:ILA2:

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
lastmember

yes it is explained in the help, thanks

Share this post


Link to post
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
Sign in to follow this  

×