look for an operator

  • Thread starter Thread starter touf
  • Start date Start date
T

touf

Hi,
I've many texboxes called txt1,txt2,...,txtn, the number isn't known at the
design time because they are created by code.
I'm looking for a way to use them folowing of an iterator i
txti.text="value"

In another languages (vb6???) I used the operator &: txt&i
how does it work in vb.net?
 
Hi Touf,
I did made a sample with chekboxes yesterday, but it is of course almost the
same with textboxes.
\\\
Private myCheckbox(10) As CheckBox
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'you need for this sample to drag a button on the form
Dim start As Integer = 4
Dim top As Integer = 18
Dim i As Integer
For i = 1 To 10
myCheckbox(i) = New CheckBox
myCheckbox(i).TextAlign = ContentAlignment.MiddleCenter
myCheckbox(i).Width = 40
myCheckbox(i).Height = 20
myCheckbox(i).Location = New System.Drawing.Point(start, top)
myCheckbox(i).Text = i.ToString
myCheckbox(i).Cursor = Cursors.Hand
Me.GroupBox1.Controls.Add(myCheckbox(i))
AddHandler myCheckbox(i).Click, AddressOf myCheckBox_Click
start = start + 40
If i = 5 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub myCheckBox_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim chk As CheckBox
chk = DirectCast(sender, CheckBox)
Dim i As String = chk.Text
End Sub
Private Sub Button1_Click _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim a As New System.Text.StringBuilder
a.Remove(0, a.Length)
Dim i As Integer
For i = 1 To 10
If myCheckbox(i).Checked Then
a.Append(i.ToString)
End If
Next
MessageBox.Show(a.ToString)
End Sub
///
I hope this helps a little bit
Cor
 
touf said:
Hi,
I've many texboxes called txt1,txt2,...,txtn, the number isn't known
at the design time because they are created by code.
I'm looking for a way to use them folowing of an iterator i
txti.text="value"

In another languages (vb6???) I used the operator &: txt&i
how does it work in vb.net?

Add the textboxes to an array, then you can use i as the index in the array,
and you can give the textboxes expressive names (if you want).
 
Hi Touf,

One way is to add them to an array or ArrayList when you create them.

If you use an array, you'll be able to give it the correct type but you'll
have to keep ReDimensioning it as you go, or create it large enough for the
maximum possible and then ReDim down to size.
Dim aoTestBoxes (MoreThanEnough) As TestBox
... Add to aoTestBoxes ...
ReDim Preserve aoTestBoxes (NumTextBoxes)
aoTestBoxes (I).Text = "value"

If you use an ArrayList, it will grow as required but you'll have to cast
the items in order to use them:
Dim alTestBoxes As ArrayList
DirectCast (alTestBoxes (I), TextBox).Text = "value"

Regards,
Fergus
 
Back
Top