ListBox with checkboxes - calculations

  1. 2 weeks ago

    Val K

    Jan 9 Pre-Release Testers, Xojo Pro

    Hi, guys,

    I have a CheckList with three columns.

    The first column is a checkbox type.

    The second column contains numbers.

    The third column contains numbers too.

    I am trying to do the following:

    1. Sum together cell values from the second column for the rows whose checkboxes are checked (Good Tokens)
    2. Sum together cell values from the third column for the rows whose checkboxes are not checked (Penalties)
    3. Calculate the final score by subtracting Penalties from Good Tokens

    To achieve this, I have written the following code

    me.period=100
    
    Dim maxscore, score As Double
    
    Dim penalty As Double
    
    Dim finalScore as Double
    
    Dim i, count As Integer
    
    For i =0 To Listbox1.ListCount - 1
      If Listbox1.CellCheck(i, 0) Then
        count = count + 1
        score=score + val(ListBox1.Cell(i,1))
      End If
      
    Next
    
    For i =0 To Listbox1.ListCount - 1
      
      maxscore=maxscore + val(ListBox1.Cell(i,1))
      
    Next
    
    For i =0 To Listbox1.ListCount - 1
      If Listbox1.CellCheck(i,0)=False then
        count = count + 1
        penalty=penalty + val(ListBox1.Cell(i,2))
      End If
    Next
    
    finalScore=score - penalty
    
    //MsgBox "Check count : " + Str(count)
    
    Label1.text="Completed:" + "  "+ str(count)
    
    Label2.text="Missed:" + "  " + str(ListBox1.ListCount-count)
    
    //Label3.text="Score:" + str(score)
    
    Label6.text="Score:" + " " +  str(round(finalScore/maxscore*100)) + "%"
    
    Label7.text= str(round(finalScore/maxscore*100)) + "%"
    

    Apparently, I've done something wrong because the final results are incorrect.

    I am more than sure that the code to loop through the rows with unchecked checkboxes is incorrect.

    I would appreciate any help.

    Thank you in advance,

    Val

  2. Jeff T

    Jan 9 Pre-Release Testers Midlands of England, Europe

    Here is simplified code.. it looks reasonable
    Perhaps Val() is not giving you the values you expect..
    (locale settings may confuse things if your cells contain 3,5 versus 3.5 or vice versa)

    Dim score, penalty, finalscore As Double
    Dim i, count As Integer
    
    For i =0 To Listbox1.ListCount - 1
      If Listbox1.CellCheck(i, 0) Then  
        //good, checked
        score=score + val(ListBox1.Cell(i,1))
      else   
        //bad, unchecked
        penalty=penalty + val(ListBox1.Cell(i,2))
    
      End If
         count = count + 1   //pointless because it should be just the number of rows??
        maxscore=maxscore + val(ListBox1.Cell(i,1))
    Next
    finalScore=score - penalty
  3. Val K

    Jan 10 Pre-Release Testers, Xojo Pro

    Hi, Jeff,

    Thank you for your help.

    I will also add a couple of lines to count Reward and Penalty actions by the corresponding line count.

    Since each line has different values in the second and third rows, it makes sense to count lines, Rewards, and Penalties.

    Thank you again,

    Val

or Sign Up to reply!