Home > database >  Storing textbox input into an array and displaying it on to a list box
Storing textbox input into an array and displaying it on to a list box

Time:09-29

I need to take 5 user inputs from the text box and store them into an array, and display the values in a listbox when I press the button.

My code looks like this:

Public Class Form1
Dim values() As Integer
Dim qty(5) As Integer
Dim i As Integer = -1
Dim j As Integer


Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    TextBox3.Text = values.Min
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    If i = 4 Then Return

    i = i   1

    values(i) = CInt(TextBox1.Text)
    qty(i) = CInt(TextBox2.Text)

    ListBox1.Items.Clear()

    For j As Integer = 0 To values.Length - 1

    Next
    ListBox1.Items.Add(values(j).ToString() & vbTab & qty(j).ToString())
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    TextBox2.Text = values.Max
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox4.Text = values.Average

End Sub

End Class

Nothing seems to be working for me, any help would be appreciated.

CodePudding user response:

You should be using GetLowerBound and GetUpperBound for your array index.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim j As Integer
    For j = values.GetLowerBound(0) To values.GetUpperBound(0)
        ListBox1.Items.Add(values(j) & vbTab & qty(j))
    Next
    values(i) = CInt(TextBox1.Text)
    qty(i) = CInt(TextBox2.Text)
    i = i   1
End Sub

CodePudding user response:

Assuming that you have 2 text boxes and you need to push 5 items into your array, your mistake is order of code and missing Clear

Dim i As Integer = -1 ' start with -1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
    If i = 4 Then Return ' completed 5 items
    
    i = i   1

    values(i) = CInt(TextBox1.Text) 'in .net - use int.Parse / int.TryParse instead but this is a different question
    qty(i) = CInt(TextBox2.Text)

    ListBox1.Items.Clear() ' clear to refill NOT add more on top
    For j as Integer = 0 To values.Length - 1 ' this works for 1-dimentional array. Otherwise use Array.GetUpperBound(0)
        ListBox1.Items.Add(values(j).ToString() & vbTab & qty(j).ToString()) ' ToString
    Next

    
End Sub

May be you just want to set how many items you want to push. Then this


Dim numOfItems As Integer = 5
Dim i As Integer = -1 ' start with -1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
    If i = numOfItems - 1 Then Return 
    
    i  = 1

    values(i) = CInt(TextBox1.Text) 
    qty(i) = CInt(TextBox2.Text)

    ListBox1.Items.Clear() 
    For j as Integer = 0 To numOfItems - 1
        ListBox1.Items.Add(values(j).ToString() & vbTab & qty(j).ToString()) 
    Next
   
End Sub
  • Related