borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (How to retrieve file info)Next Topic (Browse Through Your Desktop) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Referencing objects in loops
Poster Message
arfa
Level: Guest


icon Referencing objects in loops

This may require very basic programming to overcome but I can't find any help elsewhere.

If I require to carry out the same operation on a number of the same objects, i.e. textboxes - textbox1 to textbox12 for example, how do I reference these in a loop.

e.g.
---------------------------
For f= 1 to 10

ActiveSheet.Range("C"+Trim(Str(f))).Value=Textbox(f).Value  
' naturally the textbox(f) does not work so what does?

Next
------------------------
Any help appreciated

29-10-2002 at 02:01 PM
| Quote Reply
Harrism
Level: Guest

icon Re: Referencing objects in loops

You could have an array of textboxes.

put one textbox on you form then copy and paste you will be asked if you want to create an array say yes. this will allow you to do something like this

for i = 0 to txtdata.Count - 1

     ActiveSheet.Range("C"+Trim(Str(f))).Value= txtdata(i).text

next i

be careful though i've always found them a pain in the arse especially for db interaction where each textbox is acting with a different field or table.

if your only applying action to a few objects ie less than 15 just have 15 lines of code!!!

29-10-2002 at 02:55 PM
| Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Referencing objects in loops

not sure if it helps, but...
when I have to refer to all the textboxes in a form, I use the following code:

Dim ctrl as Control

' clears all textboxes in Form1
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then ctrl.Text = ""
Next ctrl

it seeks the Controls collection, and uses the TypeOf property. You can apply it to any type of control you have in your form.

You can also create an array of controls, by giving to each control the same name, and set each Index property to an index number.
        

____________________________
Real Programmer can count up to 1024 on his fingers

30-10-2002 at 12:20 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Referencing objects in loops
Previous Topic (How to retrieve file info)Next Topic (Browse Through Your Desktop) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder