### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] ListView column summation  (Read 751 times)

#### Pe3s

• Full Member
• Posts: 203
##### [SOLVED] ListView column summation
« on: July 04, 2022, 11:10:02 am »
How can I fix the code to sum a column

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button2Click(Sender: TObject);
2. var i: Integer;
3.   item: TListItem;
4. begin
5.   for i := 0 to ListView1.Items.Count do
6.   begin
7.     i:= Item.SubItems[2];
8.
9.   end;
10. end;
11.

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button2Click(Sender: TObject);
2. var i: Integer;
3.   item: TListItem;
4. begin
5.   for i := 0 to ListView1.Items.Count -1 do
6.   begin
7.     ListView1.Items[i].SubItems[2];
8.     Form1.Caption := IntToStr(i);
9.   end;
10. end;
11.
« Last Edit: July 10, 2022, 11:58:13 am by Pe3s »

#### howardpc

• Hero Member
• Posts: 3988
##### Re: listview column summation
« Reply #1 on: July 04, 2022, 11:18:32 am »
In English a petrel is any bird of the genus Procellaria, so named from the observation that with its webbed feet it appears to be able to walk on water (see the story about Peter in the gospel of Matthew 14:29).
Can you try an alternative translation engine so we know what you mean by "petrel"?

#### Zvoni

• Hero Member
• Posts: 1255
##### Re: listview column summation
« Reply #2 on: July 04, 2022, 12:25:25 pm »
How can I sum columns in a listview.
By hand.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### Pe3s

• Full Member
• Posts: 203
##### Re: listview column summation
« Reply #3 on: July 04, 2022, 05:47:39 pm »

#### ASerge

• Hero Member
• Posts: 1940
##### Re: ListView column summation
« Reply #4 on: July 04, 2022, 09:38:24 pm »
How can I fix the code to sum a column
Code: Pascal  [Select][+][-]
1. procedure TForm2.Button1Click(Sender: TObject);
2. var
3.   Item: TListItem;
4.   Sum: SizeInt = 0;
5. begin
6.   for Item in ListView1.Items do
7.     Inc(Sum, Item.SubItems[2].ToInteger);
8.   Caption := Sum.ToString;
9. end;

#### Pe3s

• Full Member
• Posts: 203
##### Re: ListView column summation
« Reply #5 on: July 05, 2022, 08:17:55 am »
What the code that sums up columns with time in hh: nn formatting looks like ?
Code: Pascal  [Select][+][-]
1.     procedure TForm2.Button1Click(Sender: TObject);
2.     var
3.       Item: TListItem;
4.       Sum: TTime = 0;
5.     begin
6.       for Item in ListView1.Items do
7.         Sum += Item.SubItems[2].ToInteger;
8.       Caption := FormatDateTime('hh:nn', Sum);
9.     end;
10.
« Last Edit: July 05, 2022, 06:43:03 pm by Pe3s »

#### ASerge

• Hero Member
• Posts: 1940
##### Re: ListView column summation
« Reply #6 on: July 05, 2022, 09:24:00 pm »
What the code that sums up columns with time in hh: nn formatting looks like ?
Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   Item: TListItem;
4.   Sum: TDateTime = 0;
5.   Time: TDateTime;
6. begin
7.   for Item in ListView1.Items do
8.     if TryStrToTime(Item.SubItems[2], Time) then
9.       Sum += Time;
10.   Caption := FormatDateTime('hh:nn', Sum);
11. end;

#### wp

• Hero Member
• Posts: 9888
##### Re: ListView column summation
« Reply #7 on: July 06, 2022, 01:01:44 am »
Since the sum can become larger than a day I'd rather use the square bracket symbol and the fdoInterval option in FormatDateTime:
Code: Pascal  [Select][+][-]
1.  Caption := FormatDateTime('[hh]:nn', sum, [fdoInterval]);

#### Pe3s

• Full Member
• Posts: 203
##### Re: ListView column summation
« Reply #8 on: July 07, 2022, 07:11:33 pm »
Thank you very much for help