Reputation: 3311
I need to set text of some controls.
I have a Form
with some CheckBoxes
an some TextBoxes
.
In VBA
(If I have 5 TextBoxes named "TextBox1", "TextBox2", ... "TextBox5") I can use something like this:
For n = 1 To 5
Me("TextBox" & n).Text = NeededValue
Next n
I know that something like this is also possible in VB.Net
but I wasn't able to find the right syntax (and I didn't find similar codes on SO).
I've tryed using
Me.Controls()
But I can't insert control name this way
Upvotes: 0
Views: 2869
Reputation: 2180
Use For Each
and then test with TypeOf
to find all TextBoxes
in your form
like :
For Each myObject1 As [Object] In Me.Controls
If TypeOf myObject1 Is TextBox Then
TryCast(myObject1, TextBox).Text = "NeededValue"
End If
Next
Also :
Dim myText = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
For Each btn In myText
btn.Text = "NeededValue"
Next
For i As Int32 = 1 To 5
Dim Txt = Me.Controls.Find("TextBox" & i, True)
If Txt.Length > 0 Then
Txt(0).Text = "blah"
End If
Next
Or :
For i As Int32 = 1 To 5
Dim Txt = Me.Controls.Find("TextBox" & i, True)
If Txt.Length > 0 Then
Txt(0).Text = "NeededValue"
End If
Next
Upvotes: 0
Reputation: 742
Me.Controls.Find("TextBox" & n, True)
would be the similar approach to your VBA Style.
Upvotes: 4