Klaas Weerstand
Klaas Weerstand

Reputation: 77

Is there an option for changing boolean variables trough a loop in vb.net

I am trying to change different values in a for loop in vb.net. However, I couldn't find how to change the numbered boolean variables.

Dim txt1 As Boolean = False
Dim txt2 As Boolean = False
 i = i + 1
            If i < 9 Then
                'I tried to do:
                Me.Controls("txt" & i.ToString) = True
                txt(i) = True
            End If

How can I change the numbered boolean variables and what is the most efficient way to it?

Upvotes: 0

Views: 309

Answers (2)

Julie Xu-MSFT
Julie Xu-MSFT

Reputation: 340

I guess you want to iterate over the TextBox controls and then change the property of TextBox.enabled.

You can try the following methods.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each c As Control In Me.Controls
        If c.GetType.ToString() = "System.Windows.Forms.TextBox" Then
            c.Enabled = True 'You can change the TextBox Controls. Enabled to False too.
            c.Text = c.Name.ToString
        End If
    Next
End Sub

Upvotes: 0

Klaas Weerstand
Klaas Weerstand

Reputation: 77

The right way to access the booleans is in an array or a list.

Using the example of the question:

Dim bool(2) As Boolean

i = i + 1
        If i < 9 Then
            bool(i) = True
        End If

Upvotes: 1

Related Questions