Reputation: 11
Using 3.5 VB1 framework.net
I'm trying to add an element to an array
i would like to clear the listbox and display the array contents in listbox.
then add another button, then add an element to the array, from the textbox.
Ive created this painstakingly for the past 6 hours
Call clearout() ''===== Clears listbox
Dim MyNumbers(4) As Integer
Dim i As Integer
MyNumbers(0) = 1
MyNumbers(1) = 2
MyNumbers(2) = 3
MyNumbers(3) = 4
MyNumbers(4) = 5
For i = 0 To 4
ListBox1.Items.Add(MyNumbers(i))
Next i
That part works great!
It plops it right into listbox and deletes any previous entries into listbox What Ive studied so far after all these hours to make the next button is to use UBound function to find the highest element then add one to it and ReDim it with that value
Problem is I'm not sure how to write that correctly under the second button
Any help?
Upvotes: 1
Views: 2299
Reputation: 222
I'm not quite clear what you are trying to achieve, but this is my interpretation:
Button1:
clears the listbox
populates the array with numbers 1 to 5
populates the listbox with these numbers
Button 2:
adds a number to the array from a textbox
adds this new number from the array to the listbox
OR
simply increments the values in the array by 1 and append this new value to the listbox?
In any case, you need to share the array between the buttons so it must be declared with module level scope. That is, outside the button click routines.
Also, in order to preserve the values already in the array, you need to use ReDim Preserve MyNumbers(newUBound)
Hopefully the above tips will help!
PS. Does your clearout() method simply call ListBox1.Items.Clear() ? If it does, it's best just to call this inline rather than create a new method to do this.
Upvotes: 1