iterate through textboxes. is there an easyway?

  • Thread starter Thread starter Sofa_snake
  • Start date Start date
S

Sofa_snake

Hi
I have some Array cells that that I would like to iterate through to
set as values for textboxes, I have included a small example below.
If a For While loop could be utilised it would look much tidier!


Label1.Text = W(0)
Label2.Text = W(1)
Label3.Text = W(2)
Label4.Text = W(3)
Label5.Text = W(4)

Thanks Dan
 
Hi Dan,

if the label has an index attached to its name
(ie distinctlabel0, distlabel1, distinctlabel1, etc, up to
distinctlabeln),
you could do it this way:

Dim nctr As Integer
Dim n As Integer
Dim a(2) As String
Dim oLabel As Label
a(0) = "LABEL 100"
a(1) = "LABEL 200"
a(2) = "LABEL 300"
For nctr = 0 To Me.Controls.Count - 1
If TypeOf Me.Controls(nctr) Is Label Then
oLabel = Me.Controls(nctr)
If Strings.InStr(oLabel.Name, "Label") <> 0 Then
n = CType(Strings.Replace(oLabel.Name, "Label",
""), Integer)
oLabel.Text = a(n)
End If
End If
Next

p.s.
you should name your labels the same, only the indexes vary, and the
max index should be the same as that of your list.

hope this helps.

Rey
 
Dan,

(version 2008style)
\\\
Dim Labels() = {Label1,Label2, Label3, Label4, Label5}
For i = 0 to 4
Labels(i).Text = W(i)
Next
///

Cor
 
Based on your example, here is a simple example. I have not tested this
code, and it is aimed for a web page (ASPX). The code would be similar for a
windows form, however.

For i as Integer = 0 to W.Length

Dim controlName as String = "Label" + i+1
Label l = CType(Control.Find(controlName), Label)
l.Text = W(i)

Next



--
Gregory A. Beamer
MVP; MCP: +I, Se, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*************************************************
| Think outside the box! |
*************************************************
 
Sofa_snake said:
I have some Array cells that that I would like to iterate through to
set as values for textboxes, I have included a small example below.
If a For While loop could be utilised it would look much tidier!


Label1.Text = W(0)
Label2.Text = W(1)
Label3.Text = W(2)
Label4.Text = W(3)
Label5.Text = W(4)

\\\
Dim Labels() As Label = {Label1, Label2, Label3, Label4, Label5}
For i As Integer = 0 To W.Length - 1
Labels(i).Text = W(i)
Next i
///
 
Thanks everyone you were all a great help and I got something from
each of your answers
 
Back
Top