Several TexBox in a form of an array

C

Cerebrus99

Whenever I face such an issue, I find it very useful to use the Container's
Controls collection.

For instance, put all your textboxes in a container control like a Panel or
GroupBox and then iterate through the Controls collection of that Parent
Container, setting the properties as required. If that isn't possible, you
can still treat the Form as a Parent Container (Use the Me.Controls
collection in that case)
---------------------------------------
Dim Ctrl as Control
Dim i as Integer

For i = 0 To Panel1.Controls.Count - 1
Ctrl = Panel1.Controls.Item(i)
If TypeOf (Ctrl) Is TextBox Then
Ctrl.Text = ""
End If
Next
---------------------------------------
Though it doesn't appear to be so, from your example, in case you don't need
the Index nos. of the controls, using a For-each loop is even simpler.

Regards,
Cerebrus.
 
G

Guest

Hello,

I want to create several texboxes on a form and I would like to refer to
them (properties, etc) in a form of an array. Is that possible ?

Example:

Instead of
TexBox1.text=Mytab(1):TextBox2.text=Mytab(2):TexBox3.text=Mytab(3)...

I would prefer something like that:

For I=1 to 9
TexBox(I).text=Mytab(I)
Next I
 
G

Guest

Thank you but at this point I am too new to VB2005 to understand the concepts
of Parent, Container & Collection.
 
G

Guest

Your approach appeals to me because I understand the bottom of it. But I am
getting the message Name textBoxesAry is not declared. How I could fix that,
and then, how, as example, I could replace the instruction
textbox2.text="Marcel" using your array ?

I was looking for something like: TextBox(2).text="Marcel" or
textboxesary(2).text="Marcel"

For my learning experience to VB2005, I wrote a sudoku game working quite
fine, but I have to write 81 instructions for each textbox property that I
want to modify from data kept in arrays(9,9)...

As you can see, I could be stuck for ever on very simple errors messages.
 
G

Guest

Here is the type of coding that I want to replace by a For Next loop:

Public Sub TO_TEXT()

T11.Text = T(1, 1) : T21.Text = T(2, 1) : T31.Text = T(3, 1) :
T41.Text = T(4, 1) : T51.Text = T(5, 1) : T61.Text = T(6, 1) : T71.Text =
T(7, 1) : T81.Text = T(8, 1) : T91.Text = T(9, 1)
T12.Text = T(1, 2) : T22.Text = T(2, 2) : T32.Text = T(3, 2) :
T42.Text = T(4, 2) : T52.Text = T(5, 2) : T62.Text = T(6, 2) : T72.Text =
T(7, 2) : T82.Text = T(8, 2) : T92.Text = T(9, 2)
T13.Text = T(1, 3) : T23.Text = T(2, 3) : T33.Text = T(3, 3) :
T43.Text = T(4, 3) : T53.Text = T(5, 3) : T63.Text = T(6, 3) : T73.Text =
T(7, 3) : T83.Text = T(8, 3) : T93.Text = T(9, 3)
T14.Text = T(1, 4) : T24.Text = T(2, 4) : T34.Text = T(3, 4) :
T44.Text = T(4, 4) : T54.Text = T(5, 4) : T64.Text = T(6, 4) : T74.Text =
T(7, 4) : T84.Text = T(8, 4) : T94.Text = T(9, 4)
T15.Text = T(1, 5) : T25.Text = T(2, 5) : T35.Text = T(3, 5) :
T45.Text = T(4, 5) : T55.Text = T(5, 5) : T65.Text = T(6, 5) : T75.Text =
T(7, 5) : T85.Text = T(8, 5) : T95.Text = T(9, 5)
T16.Text = T(1, 6) : T26.Text = T(2, 6) : T36.Text = T(3, 6) :
T46.Text = T(4, 6) : T56.Text = T(5, 6) : T66.Text = T(6, 6) : T76.Text =
T(7, 6) : T86.Text = T(8, 6) : T96.Text = T(9, 6)
T17.Text = T(1, 7) : T27.Text = T(2, 7) : T37.Text = T(3, 7) :
T47.Text = T(4, 7) : T57.Text = T(5, 7) : T67.Text = T(6, 7) : T77.Text =
T(7, 7) : T87.Text = T(8, 7) : T97.Text = T(9, 7)
T18.Text = T(1, 8) : T28.Text = T(2, 8) : T38.Text = T(3, 8) :
T48.Text = T(4, 8) : T58.Text = T(5, 8) : T68.Text = T(6, 8) : T78.Text =
T(7, 8) : T88.Text = T(8, 8) : T98.Text = T(9, 8)
T19.Text = T(1, 9) : T29.Text = T(2, 9) : T39.Text = T(3, 9) :
T49.Text = T(4, 9) : T59.Text = T(5, 9) : T69.Text = T(6, 9) : T79.Text =
T(7, 9) : T89.Text = T(8, 9) : T99.Text = T(9, 9)

End Sub
 
T

The Confessor

Here is the type of coding that I want to replace by a For Next loop:

Public Sub TO_TEXT()

T11.Text = T(1, 1) : T21.Text = T(2, 1) : T31.Text = T(3, 1) :
T41.Text = T(4, 1) : T51.Text = T(5, 1) : T61.Text = T(6, 1) :
T71.Text = T(7, 1) : T81.Text = T(8, 1) : T91.Text = T(9, 1)
T12.Text = T(1, 2) : T22.Text = T(2, 2) : T32.Text = T(3, 2) :
T42.Text = T(4, 2) : T52.Text = T(5, 2) : T62.Text = T(6, 2) :
T72.Text = T(7, 2) : T82.Text = T(8, 2) : T92.Text = T(9, 2)
T13.Text = T(1, 3) : T23.Text = T(2, 3) : T33.Text = T(3, 3) :
T43.Text = T(4, 3) : T53.Text = T(5, 3) : T63.Text = T(6, 3) :
T73.Text = T(7, 3) : T83.Text = T(8, 3) : T93.Text = T(9, 3)
T14.Text = T(1, 4) : T24.Text = T(2, 4) : T34.Text = T(3, 4) :
T44.Text = T(4, 4) : T54.Text = T(5, 4) : T64.Text = T(6, 4) :
T74.Text = T(7, 4) : T84.Text = T(8, 4) : T94.Text = T(9, 4)
T15.Text = T(1, 5) : T25.Text = T(2, 5) : T35.Text = T(3, 5) :
T45.Text = T(4, 5) : T55.Text = T(5, 5) : T65.Text = T(6, 5) :
T75.Text = T(7, 5) : T85.Text = T(8, 5) : T95.Text = T(9, 5)
T16.Text = T(1, 6) : T26.Text = T(2, 6) : T36.Text = T(3, 6) :
T46.Text = T(4, 6) : T56.Text = T(5, 6) : T66.Text = T(6, 6) :
T76.Text = T(7, 6) : T86.Text = T(8, 6) : T96.Text = T(9, 6)
T17.Text = T(1, 7) : T27.Text = T(2, 7) : T37.Text = T(3, 7) :
T47.Text = T(4, 7) : T57.Text = T(5, 7) : T67.Text = T(6, 7) :
T77.Text = T(7, 7) : T87.Text = T(8, 7) : T97.Text = T(9, 7)
T18.Text = T(1, 8) : T28.Text = T(2, 8) : T38.Text = T(3, 8) :
T48.Text = T(4, 8) : T58.Text = T(5, 8) : T68.Text = T(6, 8) :
T78.Text = T(7, 8) : T88.Text = T(8, 8) : T98.Text = T(9, 8)
T19.Text = T(1, 9) : T29.Text = T(2, 9) : T39.Text = T(3, 9) :
T49.Text = T(4, 9) : T59.Text = T(5, 9) : T69.Text = T(6, 9) :
T79.Text = T(7, 9) : T89.Text = T(8, 9) : T99.Text = T(9, 9)

End Sub

In your general declarations (outside of any procedure), put the
following:

Friend TextBox(8, 8) As System.Windows.Forms.TextBox

In the Form.Load procedure (or in the New() procedure, if you feel
comfortable working without the safety net of the Form Designer), put the
following:

Dim A, B As Integer
For A = 0 To 8
For B = 0 To 8
TextBox(A, B) = New System.Windows.Forms.TextBox
TextBox(A, B).Size = New System.Drawing.Size(20, 20)
TextBox(A, B).Location = New System.Drawing.Point(A * 20, B * 20)
Me.Controls.Add(TextBox(A, B))
Next
Next

These won't trigger events - setting up 2d arrays which can trigger
events is beyond my ken and may not be possible - but you should be able
to read and/or modify their contents by address at runtime.

Feel free to ask if you have more questions about how, exactly, I did
that.

The Confessor
 
C

CMM

Sorry. Here is the correction:

Dim textBoxesAry As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4}

If you wanted to access it anywhere in your class you'd put it at the class
level and change the Dim to Private or Friend.

Usage:
You would then access it like any array.

textBoxesAry(1).Text = "bla"
textBoxesAry(2).Text = "bla bla"

or in a loop

For i As Integer = 0 to textBoxesAry.Length - 1
textBoxesAry(i).Text = "bla"
Next i

or

For Each textBox As TextBox in textBoxesAry
textBox.Text = "bla"
Next textBox
 
G

Guest

Wow, it's works, first of all ! It does really, in shorts instructions, what
I was looking to do.

Thank you very much again, I am just now fully depressed of how much I still
have to learn !

Marcel Saucier
Toronto, Canada
 
G

Guest

Wow again, it's working very well now. Sorry if I need very precises
instructions. I am so new into VB2003/2005. But with peoples like you and the
Confessor and others, I am going to make real progress...

Marcel from Toronto, Canada
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top