Adding list of numbers in a list box

vb6 Ireland
  • 14 years ago

    Hi,

    Has anyone any ideas on how to add numbers in a list box (lstList). I have a text box that user enters numbers into and then clicks to put them in the list box. Another control (optSum) when clicked adds these numbers together, but i can't figure it out!

    Thanks.

  • 14 years ago

    Hi,

    Add 2 command buttons (command1, command2), 1 listbox (list1), 1 text box (text1) and 1 label (label1).

    set the following captions for the command buttons
    control         caption
    command1   add to listbox
    command2   sum

    Paste the following code :

    Private Sub Command1_Click()
        List1.AddItem Text1.Text
    End Sub

    Private Sub Command2_Click()
    Dim l As Long
    Dim lSum As Long

    'MsgBox List1.ListCount

    For l = 0 To List1.ListCount - 1
        lSum = lSum + CLng(List1.List(l))
    Next
    Label1.Caption = lSum
    End Sub

















    I hope this helps.

    Cheers
    Prakash.


















  • 14 years ago
    Hi,

    What Prakash said is right.  I am adding small modification to it. 

    Before adding to list box check whether it is number or not

    if isnumeric(text1.text) then
        'add text to listbox
    else
        'give error message
    end if

    and instead of converting to long convert it to double so that we will get decimal places also.  Any way its based on ur requirement whether u need a double or u need a long.

    cdbl(list1.list(i))

    Sorry Prakash for interfering.

    Ch. V. Sudhakar



















  • 14 years ago

    Thanks Prakash

    that worked! Take care,

    Paul

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens