Question:
VB.NET if I have 10 textboxes to retrieve information can I somehow call them by say HoldArr(i) = TextBox(i)
Brian D
2007-07-29 18:16:39 UTC
So what I would like to do is if I have a bunch of textboxes say Textbox1, Textbox2 .... etc. Textbox10 Can I somehow just call them dynamically and stick their values in an array or something of the sorts? Something like if I have an array called HoldArr(), Can I cycle through 1 to 10 and have:

For i = 0 To 9 Step 1
HoldArr(i) = TextBox(1)

Next

That doesen't work though I think because TextBox() would I am guessing be treated as an array. I do also have all these text boxes in a group box so perhaps there is a way to call each textbox in the groupbox. I am trying to make some clean code so instead of manually sticking the values of each text box into an array perhaps I could just do it in a few lines of code. Any ideas?

Thanks,

Brian
Four answers:
Einstein
2007-07-29 18:50:35 UTC
1) VB.NET doesn't support controls arrays.



Your code reads:



HoldArr(i) = TextBox(1)



What is that TextBox(1) supposed to be referring to?



2) In Visual Basic .NET, default properties are not supported, unless they take arguments:



Incorrect: strText = Text1



Correct: strText = Text1.Text



__________________________

__________________________



_______________________________________



HOW TO SIMULATE CONTROL ARRAYS IN VB.NET

________________________________________



1) Here is a very simple solution that links the controls on the form to a member variable in your code, which gives you the closest thing to a VB control array, using one function call. The only constraint is that you must follow a naming convention for your controls.



NameXXX, where 'Name' is any string name you would like for this group of controls; and, XXX is an integer value—representing the index of the array it is to be assigned to.



http://www.codeproject.com/vb/net/control_arrays_in_vbnet.asp



2) This next workaround is Microsoft's solution. You can find the article in the .NET Help file. On the Help Index tab, search "Control Array." Then, find the listing entitled, "Creating control arrays in Visual Basic.NET."



Visual Basic .NET allows you to duplicate some of the functionality commonly associated with control arrays. For example, you can use delegates to bind events from multiple controls to a single event handler. However, it might be more convenient to incorporate that functionality into a single dynamic, easy-to-manage component.
timc_fla
2007-07-30 01:54:33 UTC
I'm not a .net expert however, in vb6 you can create control arrays, I don't see why Microsoft would take that functionality away.

First thing all the textboxes have to be named the same.

The simplest way to do this is to create a text box with the properties that you want common to the text boxes then copy and paste the control on the form/frame/etc.

When you attempt to paste a control on the parent control/form, VB should pop-up a message indicating that you already have a text box with the same name, do you want to create a control array.

Obviously, reply yes.

The index property will indicate its' position in the control array.

it is then accessed much as you've already indicated, TextBox(x).text.

I would also set an Enum with names based on the function of the text box so it makes it self explanatory if you refer to the text box individually.

such as...

enum TextBoxNames

tbnName

tbnAddress

tbnZip

.

.

.

end enum

good luck.
Big John Studd
2007-07-30 01:44:54 UTC
I'm not sure if you are using .NET 1.1 or 2.0, so I'll write for 1.1.



Dim hold As ArrayList = New ArrayList



'get the Tag values for each control



For Each control As Control In groupBox1.Controls

If (control is TextBox) Then

hold.Add(control.Tag)

End If

Next



'Then manipulate the temporary array



For Each obj As Object In hold

'Do work with the objects in hold

'e.g. store their string representations into a ListBox



listBox1.Items.Add(obj.ToString)

Next





Of course, you can just directly work with the Control array and ditch the extra array.
Srinivasan C
2007-07-30 01:49:28 UTC
Use "Control Array" by using index property of the Text box



1st TextBox's Name "TextBox" index "0"

2nd TextBox's Name "TextBox" index "1"

3rd TextBox's Name "TextBox" index "2"

.....

N TextBox's Name "TextBox" index N-1





Try it


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...