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

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.