Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through a set of textbox controls 1

Status
Not open for further replies.

vietnam97

Programmer
Sep 18, 2000
48
I have a set of 3 textbox controls with names: Box1, Box2, Box3. Can I write a for loop to set their properties say (enable = true, visible = true, etc.)? You might advice me to use the control array, but in my situation I have to keep these controls separated. Note that the control names are very similar with the intention to use a for loop.

Bao
 
I'd be interested to hear why you have to keep the controls "seperated". You cannot loop through the controls by name. Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
I have an access dabatase table with the field names matching those of the text boxes. Values entered by the user on these text boxes will be populated into the table. Since the field names cannot take ( ), thus I cannot use control array text box.

Bao
 
vietnam97,
The control array would still work for you . . . just store the field name in the Tag property of the control in the conrol array and use that to determine what field to write to.
If you are really dead set against Control Arrays, then remember that each form has a control collection that you can read at run time. Just loop through that collection and set your values as needed.
for example . . .


dim objControl as Control
for each objControl in form1.controls
If typeof objControl is Textbox then
objControl.enable = true
objControl.visible = true
end if
next objTextBox


Remember that this is a simple example and that you will not only have textbox controls in the forms collection. I still think that a simple control array will be easier for what you are trying to do, but that is just my opinion. ;-)
- Jeff Marler
(please note, that the page is under construction)
 
Thanks Jeff,

I think it's a good idea to use Tag properties for field names. However, I am using the Tags for other purposes. I'll keep this in mind when rewritting the application. For now, I guess looping thru the controls collection to set the textboxes' properties is the only way to go.

Bao
 
At form load you could put those textboxes into an array of textboxes.

Dim TextBoxes(1 to 3) as textboxes

set textBoxes(1) = Box1
set textBoxes(2) = Box2
set TextBoxes(3) = Box3

Then you can loop through the array which will loop through the textboxes
 

Sorry you should dim the array as

Dim TextBoxes(1 to 3) as textbox

But a control array is preferable
 
Jeff

Two fout :)

Dim objControl As Control
For Each objControl In Form1.Controls
If TypeOf objControl Is TextBox Then
objControl.Enabled = True
'objControl.Enable = True
objControl.Visible = True
End If
Next objControl 'objTextBox
Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Eric,
Opps! If I could type, I'd be dangerous! Thanks for the corrections! X-) - Jeff Marler B-)
 
vietnam97,
If you are interested in knowing more about this, I could post a more elegant solution to what you are trying to do . . . that is if you are interested. If you are completely satisfied with the control collection of the form, then I won't waste the space up here. - Jeff Marler B-)
 
Hi Jeff,

I would be interested in your other solutions. My intention is to work with the set of textboxes on the frame and don't have to go through all other unrelated controls in the collection if possible.

Thanks,
Bao
 
You can still use the TAG property for identification - IF you set up the entries in the TAG property, it can be used as a poor man's "property bag". Just make all of the entries of the form:
[PropertyName]=PropertyValue:

When checking for the whatever, see if the 'property' you want/need is listed. If so, the value follows up to the ":".

Need some investment (read WORK) to set up, but allows quite a bit of info to be vailable in the TAG property, not JUST a single item. But then that's why Ms. Billy gave us the TAG to begin with (use as necessary?).


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 

vietnam97,

OK, check back in a few days here . . . when I have the code for my other solution, I will post it. I think that you'll like it . . . :)


MichaelRed,

Your phrase, "poor man's property bag" gave me an idea. Why not use a property bag to store multiple pieces of data in the Tag attribute (if its appropriate of course). Remember that property bags (in VB6) have a contents property that returns a Byte Array (which can be treated like a string) the contains all of the data in the property bag. Think of something like this . . .


dim objPropBag as PropertyBag
dim bytData() as Byte


Set objPropBag = New PropertyBag


'** Pack the data up so it can be
'**stored in the tag property!

with objPropBag
.WriteProperty "Prop1", vntData1
.WriteProperty "Prop2", vntData2
.
.
.
.WriteProperty "PropN", vntDataN
End with

objControl.Tag = objPropertyBag.contents


'** Now, to read the values!
'** Note that we have to convert it from a string to
'** a byte array since we can not directly set the
'** proerty bag's contents attribute to a string.

bytData = objControl.Tag
objPropertyBag.Contents = bytData

with objPropBag
vntData1 = .ReadProperty("Prop1")
vntData2 = .ReadProperty("Prop2")
.
.
.
vntDataN = .ReadProperty("PropN")
End with



Just a quick thought, but I think that it has some potential. Thanks for the seeding the idea MichaelRed! :) I will post a more robust idea in here later (a few days) or I will post it as a FAQ. Later! - Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top