Community discussion forum

listbox items to array elements?

  • 5 years ago
    Hi does anyone know how to assign individual listbox items to invidual elements of an array?

    Part of my program lets the user manually add numeric values to a list box (from a textbox), one at a time. The values stored in the list box are then used to calculate some statistics. These statistics include the minimum value, maximum value, and the range. They therefore need to be numerically sorted prior to the calculations, which isn't a problem.

    When the user clicks the 'Calculate' button, I need the code to first assign all list box items into an array so that they can then be sorted.

    Any ideas?

    I've only been using VB for a couple of weeks now, so I'm (slowly) getting to grips with it!

    Cheers
  • 5 years ago


    Pardon me asking - why are you using a listbox to store the items entered ? Or do you mean the user picks items from the list box that you use in your calculation ?

    If you are prompting the user to enter a number of items of data, then if it is always the same number of items then why not put a tesxt bos on the form for item needed, then the calculate button.

    If the number of items varies the have a text box plus your calculate button plus a 'next item' button so your program know when to calculate.

    Hope this helps
  • 5 years ago

    off my head something like


    Code:

    Dim arrayA as Array
    Dim i as Integer


    for i = 0 To listBox1.items.count
    ' //add to array
    arrayA(i) = listBox1.Items(i).text
    next i


  • 5 years ago

    That seems to be along the right lines, does the following ring any bells?


    Private Sub cmdAddManual_Click()


    dim aValues() as String    'the array
    dim i as integer



                   For i = 0 To lstValues.ListCount
                   
                        lstValues.AddItem txtAddManual.text
                        aValues(i) = lstValues.List(i)
                   
                   Next i


    End Sub


    This however doesn't work unless I declare the size of the array (e.g. aValues(10)), it just returns a 'Subcript out of range' error. I need the user to be able to input as many or as few values as they like. I've tried declaring aValues(i) but it doesn't like it :-(


    Also I think there may be something wrong with the loop. As the user manually adds value after value, this happens:


    first value : lists in the list box once (as expected!)
    second value: lists in the list box twice.
    third value: lists in the listbox 4 times.
    forth value: lists in the listbox 8 times.
    and so on.


    e.g. entering 1, 4, and 6 would appear as:


    1
    4
    4
    6
    6
    6
    6


    in the listbox


    hmm...


    Thanks for your help

  • 5 years ago

    sorry i was in .net thinking, heres some code, now imagine the user has added all the numbers to the listbox, ive added these in form load just as a demo:


    Private Sub Form_Load()
    List1.AddItem ("1")
    List1.AddItem ("2")
    List1.AddItem ("3")
    List1.AddItem ("4")
    List1.AddItem ("5")
    List1.AddItem ("6")
    End Sub


    now for what you want to add all items to the array, notice how i redim the array to make it a new length so it takes all the items in the listbox at runtime


    Code:

    Dim ArrayItems() As String


    Private Sub Command1_Click()
    '//ADD ITEMS INTO ARRAY
    '//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAT FOR SIZE
    ReDim ArrayItems(List1.ListCount)


    '//NOW ADD ITEMS
    For i = 1 To List1.ListCount
       List1.ListIndex = i - 1
       ArrayItems(i) = List1.Text
    Next i
    End Sub



    now just to check i have all the entrys in the array i run this code off a button.


    Code:

    Private Sub Command2_Click()
    '//CHECK ITEMS IN ARRAY
    For i = 1 To List1.ListCount
       MsgBox (ArrayItems(i))
    Next i
    End Sub

  • 5 years ago
    i think that's solved it cheers mate

Post a reply

Enter your message below

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

Want to stay in touch with what's going on? Follow us on twitter or Facebook!