I have to create a form that I enter the price of gas each month over a year. I enter the price in a text box and click the enter button to send the price to a list box immediatley and use an array. I have the following code but I don't think it's working with the array correctly as it will let me enter the information but doesn't stop letting me enter after the 12 prices. I am really having a hard time trying to figure out how to set up this input for an array and make it all work.

Dim priceArray(11) As Decimal
    Dim price As Decimal
    Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
        
            If enterPriceTextBox.Text <> String.Empty Then
                gasPricesListBox.Items.Add(enterPriceTextBox.Text)
                enterPriceTextBox.Clear()
            End If
       
    End Sub

Do you need an index to set the destination index value in the priceArray.

Dim Idx as Integer = 0

Then, On the enterButton_Click yopu need to add the value to the array like

priceArray(Idx) = Ctype(enterPriceTextBox.Text, Decimal) ' This can fail if the entered value is not numeric

Once entered in the array, you must increment the index to the next value and, if is greater than the upper bound of the array then disable the enterButton button to prevent further values being entered like

If Idx > 11 then
   enterButton.Enabled = False
End If

Hope this helps

This article has been dead for over six months. Start a new discussion instead.