Trouble Using Listbox

New to xojo and programming. Am practicing using listbox. In the lists box, there are four columns. I use the following code to sum the content of each column i.e. 0,0 to 30,0; 0,1 to 30,1; 0,2 to 30,2; 0,3 to 30,3. The contents of each columns are summed perfectly. However, the challenge is when I was trying to sum the results of the 4 columns the result appears to be in string form. E.g, 1000+1000, the result will appear 10001000 instead of 2000. I went through the forum and haven’t found something to get me through. Kindly help me out.

Dim total1, total2, total3, total4, total5 As Double
Dim i as integer
For i = 0 to ListBox1.ListCount-1

//column1
total1 = total1 + Val(ListBox1.Cell(i,0))
Label1.value = Format(total1, “$###,###0.00)

//column2
total2 = total2 + Val(ListBox1.Cell(i,1))
Label2.value = Format(total2, “$###,###0.00)

//column3
total3 = total3 + Val(ListBox1.Cell(i,2))
Label3.value = Format(total3, “$###,###0.00))

//column 4
total4= total4+ Val(ListBox1.Cell(i,3))
Label4.value = Format(total4, “$###,###0.00))

Label5.value= Label1.value + Label.value + Label3.value + Label4.value

You can use total5 to add total1, 2, 3 and 4 then use the same Format to put the string representation into the Label.

Note: Label.Value is now deprecated since Xojo 2020r2, you can use Label.Text

Original the text property of a Label was called .text, eg aLabel.text = „some text“

Then Xojo decided it would be „easier“ for beginners to call everything the value of the control so the text became „the value of the label“.

Note that label.value is NOT the same as Val(label)

As you have found calling the text of a label „value“ is stu… confusing as hell. So after a huge outcry and with great reluctance the label‘s „value“ was again renamed to text.

So if you „add“ strings you concatenate them: “1” + “1” = “11”

What you want to do is add their numerical values and turn the resulting number into a string:

ResultField.text = Str( val(“1”) + val(“1”) ) // =2

1 Like

Thank you Markus.
I replaced all “.value” with “.text” from label 1-5. I haven’t got the result concatenated, but neither have it added to sum. Instead what I got in result label was “0”. Below is the code.

Label5.text = str(Val(label1.text) + Val(label2.text) + Val(label3.text) + Val(label.text))

Thanks

As you have Total1-5 as doubles which hold the data why don’t you simply add those variables together and display the result in label5?

Total5 = Total1 + Total2 + Total3 + Total4
Label5.Text = Total5.Tostring("$###,##0.00")

You forgot a 2 in that line of code, so that should not compile.

You can simply do

Label5.text = Str( Total1 + Total2 + Total3 + Total4 )

or use Format which applies a mask to the value and returns it as a string

Label5.text = Format( Total1 + Total2 + Total3 + Total4, "$###,##0.00" )

(I prefer the old style Format to the new style .toString)

Remember:
you have the display stuff (Strings) and the Computer values (Integer, Double, Currency).

As a user, you only see numbers as Strings.

As a developer, you do your maths (Additions, Substractions, Divides / Multiply…) on Computer values (Integer, Double, Currency), then display them as Strings.

Things are certainly more clear now. (I hope so)

FWIW, the reason this didn’t work is that Val sees the non-numeric $ at the beginning of each value and just returns 0.

Use Wayne’s advice.

Many thanks to all. It works fine now. Am very much proud of you.