I have aseries of textboxes namely txtAlpha1, txtAlpha2, txtAlpha3 etc and txtBravo1, txtBravo2, txtBravo3 etc.

I am trying to loop thro these by doing the following but of course it does not work.

For i = 1 To 25
            If txtAlpha & i & .Text = txtBravo & i & .Text + 1 Then
                'do something
            End If

I am a novice, so any gentle help would be good.
Thanks
Sandy

Recommended Answers

All 4 Replies

I have aseries of textboxes namely txtAlpha1, txtAlpha2, txtAlpha3 etc and txtBravo1, txtBravo2, txtBravo3 etc.

I am trying to loop thro these by doing the following but of course it does not work.

For i = 1 To 25
            If txtAlpha & i & .Text = txtBravo & i & .Text + 1 Then
                'do something
            End If

I am a novice, so any gentle help would be good.
Thanks
Sandy

Please specify what type of output do you want from those textboxes so that we will be able to come up with a specific solution. thanks

The user will input integers into the text boxes.
The If part is basically saying:

If the value in txtBravo1 is less than the value in txtAlpha1 then
'Do something

Then the loop would hopefully move on to compare the values in txtBravo2 and txtAlpha2.

Hope that makes sense.

Sandy

You already noticed that you couldn't do it in the way you first tried out. VB.NET doesn't have control arrays, it has control collections instead.

Here's a one way to find matching text boxes. It uses Tag property and Name property to identify matching Alpha and Bravo text box

' Use tag property to give an unique number to text boxes. Tag is used to match Alphas and Bravos
txtAlpha1.Tag = 1
txtAlpha2.Tag = 2
txtBravo1.Tag = 1
txtBravo2.Tag = 2

' Here's the code to find matching text boxes
Dim oControlAlpha As Control
Dim oControlBravo As Control
Dim TagValueAlpha As Integer

' Loop all the controls in the container i.e. form in this case
For Each oControlAlpha In Me.Controls
  ' Check for a TextBox control
  If TypeOf (oControlAlpha) Is TextBox Then
    ' Is this Alpha?
    If CType(oControlAlpha, TextBox).Name.IndexOf("txtAlpha") >= 0 Then
      ' Now oControl is an Alpha text box
      ' Get that tag property for this Alpha and find a matching bravo
      TagValueAlpha = CInt(CType(oControlAlpha, TextBox).Tag)
      For Each oControlBravo In Me.Controls
        ' Check for a TextBox control
        If TypeOf (oControlBravo) Is TextBox Then
          ' Is this Bravo?
          If CType(oControlBravo, TextBox).Name.IndexOf("txtBravo") >= 0 Then
            ' This is a Bravo. Is this a match with the Alpha?
            If TagValueAlpha = CInt(CType(oControlBravo, TextBox).Tag) Then
              ' WE HAVE A MATCH
              ' Now, do the rest of your code
              ' Remember to cast the controls to TextBoxes, like
              ' CType(oControlBravo, TextBox).Text gives you the Text property value
            End If
          End If
        End If
      Next
    End If
  End If
Next

You already noticed that you couldn't do it in the way you first tried out. VB.NET doesn't have control arrays, it has control collections instead.

Here's a one way to find matching text boxes. It uses Tag property and Name property to identify matching Alpha and Bravo text box

' Use tag property to give an unique number to text boxes. Tag is used to match Alphas and Bravos
txtAlpha1.Tag = 1
txtAlpha2.Tag = 2
txtBravo1.Tag = 1
txtBravo2.Tag = 2

' Here's the code to find matching text boxes
Dim oControlAlpha As Control
Dim oControlBravo As Control
Dim TagValueAlpha As Integer

' Loop all the controls in the container i.e. form in this case
For Each oControlAlpha In Me.Controls
  ' Check for a TextBox control
  If TypeOf (oControlAlpha) Is TextBox Then
    ' Is this Alpha?
    If CType(oControlAlpha, TextBox).Name.IndexOf("txtAlpha") >= 0 Then
      ' Now oControl is an Alpha text box
      ' Get that tag property for this Alpha and find a matching bravo
      TagValueAlpha = CInt(CType(oControlAlpha, TextBox).Tag)
      For Each oControlBravo In Me.Controls
        ' Check for a TextBox control
        If TypeOf (oControlBravo) Is TextBox Then
          ' Is this Bravo?
          If CType(oControlBravo, TextBox).Name.IndexOf("txtBravo") >= 0 Then
            ' This is a Bravo. Is this a match with the Alpha?
            If TagValueAlpha = CInt(CType(oControlBravo, TextBox).Tag) Then
              ' WE HAVE A MATCH
              ' Now, do the rest of your code
              ' Remember to cast the controls to TextBoxes, like
              ' CType(oControlBravo, TextBox).Text gives you the Text property value
            End If
          End If
        End If
      Next
    End If
  End If
Next

Thank you very much.
exactly what I required.
Sandy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.