Jump to content

Listview & ListViewItems


ca143508
 Share

Recommended Posts

Guys,

I am not sure if it is possible but I would like to get the combined value of a column. The values are for example

2007/12/25|$50.00|Xmas Gift

2007/12/24|$35.50|Lunch

So I'd like to get the total of column 2 ($85.50).

Any tips???

Cheers,

Mike.

Link to comment
Share on other sites

Ok, I'm bored, so have a look at this:

#include <GUIConstants.au3>
#include <GUIListView.au3>

GUICreate('Add Columns', 300, 200)

$lv = GUICtrlCreateListView('Date|Amount|Item', 10, 10, 280, 144)

GUICtrlCreateListViewItem('2007/12/25|$50.00|Xmas Gift', $lv)
GUICtrlCreateListViewItem('2007/12/24|$35.50|Lunch', $lv)
GUICtrlCreateListViewItem('2007/12/23|$60.00|Hookers', $lv)
GUICtrlCreateListViewItem('2007/12/23|$35.00|Hooker''s Lunch', $lv)

_GUICtrlListViewSetColumnWidth($lv, 0, 70)

$btn = GUICtrlCreateButton('Get Total!', 10, 164, 60, 26)
$lbl = GUICtrlCreateLabel('Total is:  $00.00', 80, 164, -1, -1, $SS_CENTERIMAGE + $SS_CENTER)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn
            GUICtrlSetData($lbl, 'Total is:  $' & _addit())
    EndSwitch
WEnd

Func _addit()
    Local $num_total = 0
    For $i = 0 To _GUICtrlListViewGetItemCount($lv)
        $num = StringTrimLeft(_GUICtrlListViewGetItemText($lv, $i, 1), 1)
        If $num = '' Then ContinueLoop
        $num_total += $num
    Next
    Return StringFormat('%.2f', $num_total)
EndFunc
Link to comment
Share on other sites

Ok, I'm bored, so have a look at this:

#include <GUIConstants.au3>
#include <GUIListView.au3>

GUICreate('Add Columns', 300, 200)

$lv = GUICtrlCreateListView('Date|Amount|Item', 10, 10, 280, 144)

GUICtrlCreateListViewItem('2007/12/25|$50.00|Xmas Gift', $lv)
GUICtrlCreateListViewItem('2007/12/24|$35.50|Lunch', $lv)
GUICtrlCreateListViewItem('2007/12/23|$60.00|Hookers', $lv)
GUICtrlCreateListViewItem('2007/12/23|$35.00|Hooker''s Lunch', $lv)

_GUICtrlListViewSetColumnWidth($lv, 0, 70)

$btn = GUICtrlCreateButton('Get Total!', 10, 164, 60, 26)
$lbl = GUICtrlCreateLabel('Total is:  $00.00', 80, 164, -1, -1, $SS_CENTERIMAGE + $SS_CENTER)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn
            GUICtrlSetData($lbl, 'Total is:  $' & _addit())
    EndSwitch
WEnd

Func _addit()
    Local $num_total = 0
    For $i = 0 To _GUICtrlListViewGetItemCount($lv)
        $num = StringTrimLeft(_GUICtrlListViewGetItemText($lv, $i, 1), 1)
        If $num = '' Then ContinueLoop
        $num_total += $num
    Next
    Return StringFormat('%.2f', $num_total)
EndFunc
XCAL - you the man!
Link to comment
Share on other sites

Came cross this thread while trying to find an eays way to add multiple items together.

The example above gave me a great start, so im posting the slightly modified code for adding multiple selected items below.

CODE

#include <GUIConstants.au3>

#include <GUIListView.au3>

GUICreate('Add Columns', 300, 200)

$lv = GUICtrlCreateListView('Date|Amount|Item', 10, 10, 280, 144, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER))

GUICtrlCreateListViewItem('2007/12/25|$50.00|Xmas Gift', $lv)

GUICtrlCreateListViewItem('2007/12/24|$35.50|Lunch', $lv)

GUICtrlCreateListViewItem('2007/12/23|$60.00|Hookers', $lv)

GUICtrlCreateListViewItem('2007/12/23|$35.00|Hooker''s Lunch', $lv)

_GUICtrlListViewSetColumnWidth($lv, 0, 70)

$btn = GUICtrlCreateButton('Get Total!', 10, 164, 60, 26)

$lbl = GUICtrlCreateLabel('Total is: $00.00', 80, 164, -1, -1, $SS_CENTERIMAGE + $SS_CENTER)

GUISetState()

While 1

$msg = GUIGetMsg()

Switch $msg

Case $GUI_EVENT_CLOSE

Exit

Case $btn

GUICtrlSetData($lbl, 'Total is: $' & _addit())

EndSwitch

WEnd

Func _addit()

Dim $total

Local $sa = _GUICtrlListViewGetSelectedIndices($lv,1)

For $i = 1 To UBound($sa) - 1

$num = StringTrimLeft(_GUICtrlListViewGetItemText($lv, $sa[$i], 1), 1)

$total = $total + $num

Next

Return StringFormat('%.2f', $total)

EndFunc

Edited by frodo
Link to comment
Share on other sites

Change For $i = 1 To $sa[0] to For $i = 1 To UBound($sa) - 1. Otherwise you'll get an error if nothing is selected.

Ooops, good point, i was in a rush last night with my own code and solved it there,.

Changed in original post!

Thanks for the reminder, err kick in the pants

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