Home All Groups Group Topic Archive Search About

Creating an array of textbox objects

Author
12 Nov 2006 9:26 PM
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

Author
12 Nov 2006 10:57 PM
robarahz
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
Author
13 Nov 2006 5:11 AM
Cor Ligthert [MVP]
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



Show quoteHide quote
"Hector M Banda" <hec***@mxlweb.com> schreef in bericht
news:eHzczEqBHHA.4256@TK2MSFTNGP04.phx.gbl...
> 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
>
>
Author
13 Nov 2006 5:10 PM
Shane
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
Author
13 Nov 2006 5:29 PM
BK
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.