Results 1 to 6 of 6

Thread: Calculating a listbox? Help..

  1. #1

    Thread Starter
    New Member jamestuck's Avatar
    Join Date
    Apr 2001
    Location
    Coventry
    Posts
    9
    Hi i have a list box that gets items added to it, the items that are added are numbers. Once i have finished adding the numbers i need a function to total the listbox numbers up.

    Please note i need this to work in any number of items within the list box.

    Thanks for all you help.

    James
    James T

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    Private Function listsum(list As ListBox)
    Dim i As Integer
    Dim sum As Long
    For i = 0 To list.ListCount
    sum = sum + Val(list.list(i))
    Next i
    listsum = sum
    End Function
    Usage
    Code:
    dim sum as long
    sum = listsum(list1)
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    New Member jamestuck's Avatar
    Join Date
    Apr 2001
    Location
    Coventry
    Posts
    9

    Dont understand?

    Hi sorry could you write it in a command button for me im not sure what usage etc is?

    Thanks

    Private Sub cmd1_Click()
    what goes in here?
    End Sub
    James T

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Total newbie, a. No problem. Put this into the general section of the code.
    Code:
    Private Function listsum(list As ListBox)
    Dim i As Integer
    Dim sum As Long
    For i = 0 To list.ListCount
    sum = sum + Val(list.list(i))
    Next i
    listsum = sum
    End Function
    Under button click event
    Code:
    Private Sub cmd1_Click() 
    dim sum as long
    sum = listsum(nameofyourlistbox)
    End Sub
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Or if you don't want to use a function:
    Code:
    Private Sub cmd1_Click() 
    Dim i As Integer
    Dim sum As Long
    For i = 0 To list.ListCount
    sum = sum + Val(list.list(i))
    Next i
    'sum contains sum
    End Sub
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6

    Thread Starter
    New Member jamestuck's Avatar
    Join Date
    Apr 2001
    Location
    Coventry
    Posts
    9

    Talking Brilll!!!!!!

    Thats great i was totalling the list every time something was added to not not all in one go. Your a star!!! I love VB and i have only been learning it at university a week!!!

    James
    James T

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width