Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Adding the numeric values in a combobox ?

if my combobox has 5 entries all numeric  eg:

1500
2000
25
50
1230

how can I add these values to give me a total (integer) ?

I have this code but it only gives me the value of the last entry in the combobox.....

for i := 0 to combobox2.items.count -1 do
begin

s := combobox2.itemindex + 1;
Combobox2.itemindex := ( s );
total := Strtoint(combobox2.items[combobox2.itemindex]);
label8.caption := Inttostr(total);
inc ( s );


As you can see here, label8.caption gets changed for each item in the combobox instead of adding them all up.  any help would be appreciated

thanks

Mark
0
Mark Brady
Asked:
Mark Brady
  • 3
  • 2
1 Solution
 
SlaughCommented:
label8.Caption := 0;
for i := 0 to combobox2.items.count -1 do
 begin
  combobox2.ItemIndex := i;
  label8.caption := inttostr(strtoint(label8.caption) + strtoint(combobox2.items[combobox2.itemindex]));
 end;
0
 
SlaughCommented:
ack, that first line should read: label8.Caption := '0';
0
 
Slick812Commented:
procedure TForm1.sbut_AddComboClick(Sender: TObject);
var
i, total: Integer;
begin
Total := 0;
for i := 0 to combobox1.items.count -1 do
begin
total := total+Strtoint(combobox1.items[i]);
end;
label3.caption := Inttostr(total);
end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mark BradyPrincipal Data EngineerAuthor Commented:
Thanks slaugh !   Perfect answer.

Mark
0
 
SlaughCommented:
Just noticed that it can be shortened to:

label8.Caption := '0';
for i := 0 to combobox2.items.count -1 do
 label8.caption := inttostr(strtoint(label8.caption) + strtoint(combobox2.items[i]));
0
 
Mark BradyPrincipal Data EngineerAuthor Commented:
Good observation !  Thanks

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now