visual basic: looping through textboxes

Discussion in 'Programmer's Corner' started by ecka333, Apr 13, 2013.

  1. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    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:

    Code ( (Unknown Language)):
    1. _serialPort.Write({255, 253, 1, 0}, 0, 4)
    2.         TextBox1.Text = _serialPort.ReadByte
    3.  
    4.         _serialPort.Write({255, 253, 2, 0}, 0, 4)
    5.         TextBox2.Text = _serialPort.ReadByte
    6.  
    7.         _serialPort.Write({255, 253, 3, 0}, 0, 4)
    8.         TextBox3.Text = _serialPort.ReadByte
    9.         ........
    10.  
    How can i loop throug them with FOR loop and fill bohes automatically. I am using visual basic 2010.
     
  2. FroceMaster

    Member

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

    Code ( (Unknown Language)):
    1.  
    2. for i=0 to 47
    3. _serialPort.Write({255, 253, i+1, 0}, 0, 4)
    4.         TextBox1(i).Text = _serialPort.ReadByte
    5.  
    6.         next
    7.  
     
  3. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    If i rename My first textbox to TextBox1(i) visual basic says: "property value is not valid"
     
  4. FroceMaster

    Member

    Jan 28, 2012
    402
    4
    Works fine i VB 6, Have not never version.
    But i think it can be fixed.
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    Code ( (Unknown Language)):
    1.  
    2.  
    3.     Dim i As UInteger
    4.  
    5.         For i = 1 To 2
    6.             Me.Controls.Find("TextBox" + CStr(i), True)(0).Text = "Foo"
    7.  
    8.         Next
    9.  
     
  6. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    Great, it works! Thank you, Spinnaker. But can you explain your code a little?
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    I think it would be better if you read the documentation for Control.ControlCollection.Find
     
  8. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2. Private Sub CheckPorts()
    3.         Dim PortList() As String
    4.         PortList = SerialPort.GetPortNames()
    5.         Dim j As Byte = 0
    6.  
    7.         While j < 15
    8.             If PortList(j) <> "" Then
    9.                 ComboBox5.Items.Add(PortList(j))
    10.                 If j = 0 Then
    11.                     ComboBox5.Text = PortList(j)
    12.                 End If
    13.             End If
    14.             j = j + 1
    15.         End While
    16.     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
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
Loading...