Bardia Bardia - 1 month ago 6
Vb.net Question

Visual Basic Iterative enabling of Textbox's

Si I'm working on an assignment where I have 10 RadioButtons indicating how many contesters I have, and depending on what I pick between 1 to 10, I need that many of my corresponding TextBoxes to be enabled so I could fill it with names!

Is there a way for me to make a For loop between 1 and the number I picked from the RadioButton and say something like

For i = 0 to Size
{
TextBox&i.Enabled = True
}


Since my TextBoxs are called TextBox1 to TextBox10

I know you can add strings together using &, but how can I do that for an object name?

As of right now I literally have the dumbest way of doing it, which is a click event inside each RadioButton that manually enables the correct number of TextBoxes...

Thank you in advance!

Answer

You can iterate over all controls like this:

For Each ctr In Me.Controls
Dim indx As String = ctr.Name
If TypeOf (ctr) Is Textbox Then
 ' Now compare the name with TextBox&i and do smth
End If
Next
Comments