By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Creating an array of textbox objects

P: n/a
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
Nov 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 12 '06 #2

P: n/a
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

"Hector M Banda" <he****@mxlweb.comschreef in bericht
news:eH**************@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


Nov 13 '06 #3

P: n/a
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

Nov 13 '06 #4

P: n/a
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.

Nov 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.