How to name textbox with index i
Hi.
I use VB 2005 express edition. I want to display the strings in textbox1,textbox2 and textbox3 by using a loop. However there is error "Class 'System.Windows.Forms.TextBox' cannot be indexed because it has no default property".
Below is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 1 To 32
MsgBox(TextBox1(i).text)
Next i
End Sub
Anyone who can teach me to solve this problem?
[556 byte] By [
imname] at [2007-11-20 9:58:01]

# 1 Re: How to name textbox with index i
If you want to display a message for every textbox on your form you can do something like this to get all textboxes:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim control1 As Control
For Each control1 In Me.Controls
If TypeOf (control1) Is TextBox Then
MessageBox.Show(control1.Text)
End If
Next
End Sub
Laitinen
# 2 Re: How to name textbox with index i
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _System.EventArgs) Handles MyBase.Load
Dim txtctrl As TextBox
Dim i As Integer
For i = 2 To 10
txtctrl = New TextBox()
txtctrl.Name = "TextBox" & (i)
txtctrl.Left = Me.TextBox1.Left
txtctrl.Top = Me.TextBox1.Top + (30 * i)
Controls.Add(txtctrl)
Next
End Sub
If I create my textboxes by using above code, how can i name the textbox2 to textbox10?
As an example, I want to display the sum of TextBox2 and TextBox4 in TextBox10. I found that i cannot write my code as below:
TextBox10.text = TextBox2.text + TextBox4.text
imname at 2007-11-10 3:09:49 >

# 3 Re: How to name textbox with index i
As an example, I want to display the sum of TextBox2 and TextBox4 in TextBox10. I found that i cannot write my code as below:
TextBox10.text = TextBox2.text + TextBox4.text
Try: TextBox10.text = TextBox2.text & TextBox4.text
Or if you want to add a space between the two:
TextBox10.text = TextBox2.text & " " & TextBox4.text