Creating an array of textbox objects

  • Thread starter Thread starter Hector M Banda
  • Start date Start date
H

Hector M Banda

Hi all,
I am working on a project where I have a set of textboxs and some butons and
I would like to have a way of keeping the textboxes using an array to make
easier my data input.

THis is what I am trying to do:

Create an array of 4, 5 or more textboxes and collect the information using
a for-next loop

for t =1 to 4
infosr &= TEXTBOX(T).TEXT
next t

messagebox.show(infostr)

--------------


I am using VB2005

Thanks,

-hb
 
Assuming your textbox names start with txt, I think this would work...

Dim Infosr as String
Dim myCont as New Control

For Each myCont in Me.Controls

If myCont.Name.StartsWith("txt") Then
Infosr &= myCont.Text
End If

Next myCont
 
Hector,

The most easy way in my idea,

dim myControlArrayToUse() as Control = {TextBox1, Txsbal, Texbox3,
WhateverName}
dim myArray(3) as string

for i as integer from 0 to 3
myArray(i) = myControlArrayToUse(i).text
next

I hope this helps,

Cor
 
As long as you give all of your textboxes the same prefix name ...

Dim Prefix as string = "txtboxnameprefix"
for t =1 to 4
inforsr &= CType(Mel.Controls.Item(Prefix & Cstr(t),TextBox).Text
next t
 
In my opinion, you'd be much better off using a collection. For
example:

Dim ControlList As New Collection()
ControlList.Add(Me.SomeTextBox, "SomeTextBox")
.... repeat the add method for every textbox you want to do something
with

Then later in your code you could:

Dim TempTextbox As TextBox
For Each TempTextbox In ControlList
SomeVar = TempTextbox.Text
'or
TempTextbox.Value = SomeNewValue
'or
TempTextbox.Visible = SomeFunctionCallToCheckSecurity()
Next

We do this in our base forms for checking security on textboxes. It's
a simple and elegant way to handle some common functionality on a
variable number of controls. Hope this helps.
 
Back
Top