Option Explicit
 



Private Sub cmdExit_Click()
  End
End Sub

Private Sub cmdRun_Click()
Dim num(19) As Integer
Dim i As Integer
Dim sum As Integer

 grd.Rows = 2
 grd.Cols = 2
 grd.Clear
 For i = 0 To 19 Step 1
    num(i) = InputBox("Enter any integer")
    lstno.AddItem (num(i))
    Next i
    
    grd.TextMatrix(0, 0) = "ALL NO."
For i = 0 To 19
    grd.TextMatrix(grd.Rows - 1, 0) = num(i)
    grd.Rows = grd.Rows + 1
Next i
For i = 0 To 19
sum = i + num(i)
 
Next i
lblsum.Caption = sum
End Sub

Dani AI

Generated

Good points from (loop the ListBox) and (accumulate as items are added). The real bug in the OP's final loop is not the grid at all but the assignment: the code sets the total to a single expression (i + num(i)) instead of accumulating each entry. A safer, more robust approach is to treat the ListBox as the source of truth and validate/convert each string before adding it into a numeric accumulator.

A compact, resilient VB6 routine that sums numeric entries in a ListBox:

Function SumListBox(lb As ListBox) As Double
    Dim idx As Integer
    Dim txt As String
    Dim total As Double
    total = 0#
    For idx = 0 To lb.ListCount - 1
        txt = Trim$(lb.List(idx))
        If Len(txt) > 0 Then
            txt = Replace$(txt, ",", "")      ' remove thousands separators if used
            If IsNumeric(txt) Then
                total = total + CDbl(txt)
            End If
        End If
    Next idx
    SumListBox = total
End Function

Call it when needed and format the result for the label, for example:

lblsum.Caption = Format$(SumListBox(lstno), "0.00")

Notes and gotchas: in VB6 an Integer is 16‑bit (−32,768 to 32,767); prefer Long or Double for accumulators to avoid overflow. Use CDbl (or Currency for fixed‑point money) rather than Val when locale and strict parsing matter; Val can be useful but stops at the first non‑numeric character. Handle InputBox returns (empty string when Cancel pressed) and either reprompt or break out of the input loop. For large lists or frequent add/remove operations, maintain a running total (update it on add/remove) instead of recalculating the whole ListBox each time; recalc the whole list when items can be edited directly. This keeps the display accurate and prevents the assignment-vs-accumulation mistake seen above.

Recommended Answers

All 4 Replies

What is that GRID??
If you want to calculate the sum of "lstno" items, you'll need a simple loop:

For i = 0 to 19
     sum = sum + lstno.List(i)
Next i

But I think you are trying to do something more complicated, would you please describe more?

The code looks like you are entering 20 numbers.

But, it is not clear what you are doing with a Two by Two FlexGrid...

But, that last section is where you get into trouble because you are changing your loop variable "i" within the loop in a way that will most likely terminate the loop before you get very far.

For instance if the first number in your array is 20, that would be the only number acted upon and the loop would exit with i equal to 20.

If all the numbers total less than 20, i will still end up being equal to 20...

If any of the numbers are greater than 20, and the previous total stays less than 19 then, i will exit equal to the first number greater than 19...


DryCola has the correct way to sum your twenty numbers. This works because "sum" is placed into memory, then added to, then the new value is written back to the variable "sum".

Please detail how and what you want to do with the Grid...

Actually i know how to get the sum of the flexgrid,what i want to know is how to get the sum of the numbers i entered in the listbox...

You could also just make these little modifications:

Option Explicit

Private Sub cmdExit_Click()
  End
End Sub

Private Sub cmdRun_Click()
  Dim num(19) As Integer
  Dim i As Integer
  Dim sum As Integer

  sum = 0                  ' <----------- BitBlt added this
  grd.Rows = 2
  grd.Cols = 2
  grd.Clear
  For i = 0 To 19 Step 1
    num(i) = InputBox("Enter any integer")
    lstno.AddItem (num(i))
    sum = sum + num(i)      ' <----------- BitBlt added this
  Next i
    
  grd.TextMatrix(0, 0) = "ALL NO."

  For i = 0 To 19
    grd.TextMatrix(grd.Rows - 1, 0) = num(i)
    grd.Rows = grd.Rows + 1
  Next i

 ' <---------- BitBlt removed some stuff

  lblsum.Caption = sum

End Sub

There, saved you a loop. You could even fold the other loop in, but I'm not sure what you're doing with the whole grid thing.

You might also want to do a little editing of the return value of your input box. Or at least have an On Error statement at the top. Just sayin'...

Anyway, I hope that's what you were looking for...good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.