Yliah Yliah - 9 days ago 7
Vb.net Question

Looping of each 2 TextBoxes

I've been thinking of on how to loop my textboxes that has an integer as a value and insert it in a listview. My textboxes starts at Textbox2.Text and ended in Textbox41.Text. The thing is I need to check each 2 textboxes (ex. textbox2 and textbox3)and insert in a listview. I dont know how to loop the other textboxes.

Try
If TextBox2.Text <> Nothing And TextBox3.Text <> Nothing Then
Pid += 1
At = TextBox2.Text
Bt = TextBox3.Text

Table = ListView1.Items.Add(Pid)
With Table
.SubItems.Add(At)
.SubItems.Add(Bt)
End With

TextBox2.Text = Nothing
TextBox3.Text = Nothing
End If
Catch ex As Exception
MsgBox("Must be numbers!")
End Try

Answer

If your application is Windows Form, this is how you can get all integer values...

Dim intList As New List(Of Integer)()
For Each c As Control In Me.Controls
    If c.GetType Is GetType(TextBox) Then
        Dim txtControl As TextBox = CType(c, TextBox)
        If Integer.TryParse(txtControl.Text, 0) Then
            intList.Add(Convert.toInt32(txtControl.Text))
        End If
    End If
Next
Comments