confusing: for loop

homebrew311

Freshman
Joined
Oct 25, 2003
Messages
31
This might be confusing: wut if i wanted to create a series of 10 textboxes called 'textbox1', 'textbox2', etc in a for loop? is this possible? ex:

for i = 0 to 10
dim textboxX as new textbox()
next

i kno thats not the right way, but does anyone kno how to do replace the X with the # that i is currently?
 
Use an array.
Visual Basic:
Dim tb(9) As TextBox
Dim i As Integer

For i = 0 to 9
  tb(i) = New TextBox
  'set TextBox(i) properties
  Me.Controls.Add(tb(i))
Next i
 
As long as you defined them the same way I did, you could do that. You can also enumerate all TextBoxes on the form:
Visual Basic:
Dim tb As New TextBox

For Each tb In Me.Controls
  tb.BackColor = Color.Red
Next
 
this is a little more confusing: just as a test of my skills, im making a program that loads data from a file and sets that color to one of the 16 panels. I just saved the file with 'Color.Red' and different colors for 16 lines and im trying to set the properties of Panels1-16 each lineinput. heres wut i have so far:

Dim P As New Panel()
Dim C As String
FileOpen(1, "w.txt", OpenMode.Input)
For Each P In Me.Controls
C = LineInput(1)
If C = "Blue" Then
P.BackColor = Color.Blue
ElseIf C = "Red" Then
P.BackColor = Color.Red
ElseIf C = "Green" Then
P.BackColor = Color.Green
End If
Next

but u see, i want to be able to set each of the 16 lines uniquely instead of the 3 colors i have coded. like if i wanted to set the backcolor of panel1, i would set the first line in the file to 'Color.Brick'. i cant just say 'pb(i).backcolor = lineinput(1)'. how can i do this?
 
If they're from the KnownColor enum, you can use Color.FromKnownColor(C)

If the color you store isn't a known color (just some semi-random RGB values) you'll have to do it another way.

-Nerseus
 
Back
Top