473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Several TexBox in a form of an array

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):TexBo x3.text=Mytab(3)...

I would prefer something like that:

For I=1 to 9
TexBox(I).text=Mytab(I)
Next I
Feb 11 '06 #1
9 1190
CMM
textBoxesAry = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}

--
-C. Moya
www.cmoya.com
Feb 11 '06 #2
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.

Feb 11 '06 #3
Thank you but at this point I am too new to VB2005 to understand the concepts
of Parent, Container & Collection.
--
Super Basic programmer under DOS since 1983. Absolutely dummy VB.NET
programmer under Windows !
"Cerebrus99" wrote:
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.

Feb 11 '06 #4
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.
--
Super Basic programmer under DOS since 1983. Absolutely dummy VB.NET
programmer under Windows !
"CMM" wrote:
textBoxesAry = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}

--
-C. Moya
www.cmoya.com

Feb 11 '06 #5
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

--
Super Basic programmer under DOS since 1983. Absolutely dummy VB.NET
programmer under Windows !
"CMM" wrote:
textBoxesAry = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}

--
-C. Moya
www.cmoya.com

Feb 11 '06 #6
"=?Utf-8?B?TWFyY2VsIFNhdWNpZXI=?="
<Ma***********@discussions.microsoft.com> wrote in
news:49**********************************@microsof t.com:
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
Feb 11 '06 #7
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

--
-C. Moya
www.cmoya.com
"Marcel Saucier" <Ma***********@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
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.
--
Super Basic programmer under DOS since 1983. Absolutely dummy VB.NET
programmer under Windows !
"CMM" wrote:
textBoxesAry = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}

--
-C. Moya
www.cmoya.com

Feb 12 '06 #8
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
--
Super Basic programmer under DOS since 1983. Absolutely dummy VB.NET
programmer under Windows !

Feb 12 '06 #9
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
--
Super Basic programmer under DOS since 1983. Absolutely dummy VB.NET
programmer under Windows !
Feb 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Davy | last post by:
My program deal with a several level tree with several branchs. The amount of branchs from father node is not known. So I want to creat a tree with dynamic branchs. Now I use the structure:...
3
by: mjpiedade | last post by:
I just started with C#. How do I navigate on a dataset by changing the value of a textbox or other simple bound controls? Imagine that I have the customer name on a textbox and have a datagrid...
1
by: albertoalar | last post by:
I m trying to intercept a return in a texbox (Pocket pc ) when a barcode is writed. I need help thanks
3
by: Denise | last post by:
I am working with some pre-written code that dynamically builds a page with user controls based on data. I need to attach my Javascript to one of the controls on the form - I do not have access to...
0
by: Yog | last post by:
Hi, How to access the value of a Texbox column(Template field) in gridview and to update a column next to it in the Gridview...! Example:Let's say there are four columns Product,Cost,Quantity...
1
by: pgreenhill | last post by:
Hi All, I'm creating a Form ("Menu") that has 2 listboxs in it. My problem is that I can't copy the values of each listbox, into another Form's textboxs. The idea is that users can...
2
by: =?Utf-8?B?ZnJlZGR5?= | last post by:
I am learning c# and I have a problem. I would like to enter a username in form A textbox and have it apper in form B testbox. I have something like this frmlaptopChk laptop = new...
2
by: Tom | last post by:
I have a textbox on my web form were users can enter in 1 name or many names. The form then validates the name against the database. If the name is valid its then saved into a varaible to be used at...
3
by: menakavenkatesh | last post by:
i want simple coding for changing fontsize of the texbox
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.