Results 1 to 9 of 9

Thread: ********Add up numeric values in a listbox*******

  1. #1

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    Exclamation

    Easily, how can I add up numeric values in a listbox and display the total in a text field.

    i.e lets say this is my list box

    ------------------
    0.75
    0.95
    1.25
    3
    1.55
    ------------------

    how can the total or sum be found.

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Code:
    dim counter as integer
    dim tempAnswer as integer
    tempAnswer = 0
    for counter = 0 to 4
          List1.listindex = counter
          TempAnswer = tempanswer + val(list1.text)
    next counter
    text1.text = tempanswer
    I didn't have time to test that so I hope it works

  3. #3
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Code:
    dim counter as single
    dim tempAnswer as single
    tempAnswer = 0
    for counter = 0 to 4
          List1.listindex = counter
          TempAnswer = tempanswer + val(list1.text)
    next counter
    text1.text = tempanswer
    I didn't test it but it should be singles instead of integers
    NXSupport - Your one-stop source for computer help

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    woops, didn't notice that thanks dimava

  5. #5

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    why??????????

    why is the "counter = 0 to 4", why is it 0 to 4??

  6. #6
    Lively Member
    Join Date
    Jan 2000
    Location
    Dallas, TX
    Posts
    89
    It is 0 to 4 because you have 5 values in your listbox and the listindex starts with 0, therefore you have 0 to 4. To account for all 5 values.
    ande211
    VB6 SP3

  7. #7

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    Exclamation in reply what if the number of values are undetermined

    yes but what if the number on items in the listbox are undetermined and are added to everytime.

    the reason i say is because i am working or a program used to keep a total of daily sales made in a store. so everytime a receipt is calculated the total is added to the this listbox and this happens everytime a new receipt is calculated, in turn i need to calculate a daily total, for that i must find a way of adding all the values in the listbox and displaying a daily total in the text field.

    thanks

  8. #8
    Lively Member
    Join Date
    Jan 2000
    Location
    Dallas, TX
    Posts
    89
    In that case you would use 0 to list1.ListCount instead of some number. The ListCount property gives you the number of values in your Listbox. So you would say counter = 0 to List1.ListCount
    ande211
    VB6 SP3

  9. #9
    Member
    Join Date
    May 2000
    Location
    Canada
    Posts
    52
    You will use
    counter = 0 to List1.ListCount-1
    listcount starts at 1 while the list starts at 0.

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