visual basic: looping through textboxes

Thread Starter

ecka333

Joined Oct 1, 2009
76
I have 48 TextBoxes and i need to fill them with values, which i read from microcontroller throug uart. TextBoxes are named TextBox1 to TextBox48
I can write such code:

Rich (BB code):
_serialPort.Write({255, 253, 1, 0}, 0, 4)
        TextBox1.Text = _serialPort.ReadByte

        _serialPort.Write({255, 253, 2, 0}, 0, 4)
        TextBox2.Text = _serialPort.ReadByte

        _serialPort.Write({255, 253, 3, 0}, 0, 4)
        TextBox3.Text = _serialPort.ReadByte
        ........
How can i loop throug them with FOR loop and fill bohes automatically. I am using visual basic 2010.
 

FroceMaster

Joined Jan 28, 2012
708
if you rename the textboxes
like TextBox1(i).text
u get

Rich (BB code):
for i=0 to 47
_serialPort.Write({255, 253, i+1, 0}, 0, 4)
        TextBox1(i).Text = _serialPort.ReadByte

        next
 

Thread Starter

ecka333

Joined Oct 1, 2009
76
i faced yet with one problem. I tried to read available com ports on my computer and place ports list in combo box. But i get error when reading them. There is the code:
Rich (BB code):
Private Sub CheckPorts()
        Dim PortList() As String
        PortList = SerialPort.GetPortNames()
        Dim j As Byte = 0

        While j < 15
            If PortList(j) <> "" Then
                ComboBox5.Items.Add(PortList(j))
                If j = 0 Then
                    ComboBox5.Text = PortList(j)
                End If
            End If
            j = j + 1
        End While
    End Sub
visual basic gives error:
System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Troubleshooting tips:
Make sure that the maximum index on a list is less than the list sizeThe maximum index on a list must be less than the list size.
Make sure the index is not a negative number.This exception will be thrown if the index is less than zero.
Make sure data column names are correct.maybe someone could help me?

Here is error picture:
https://dl.dropboxusercontent.com/u/74736925/istrinti/visualBasicError.bmp
 
Top