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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controlling objects using a loop? 1

Status
Not open for further replies.

RSH

Technical User
Jun 15, 2000
55
US
If one has a series of objects such as Command buttons and wishes to clear the screen of same, or to make them visible can this be done without writing code for each object. The use of a for next loop would seem ideal but VB doesnt seem to like this. There may be a secret to turning all off or on.
73 RSH [sig][/sig]
 
You can use this code to disable all of the Command Buttons:

Private Sub Form_Load()
Dim ctl As Control

For Each ctl In Me.Controls
If TypeOf ctl Is CommandButton Then
ctl.Visible = False
End If
Next
End Sub [sig][/sig]
 
Thanks Mostafavi:
I sure never would have guessed that one. My for next attempts were simpler and didnt work.
73 RSH [sig][/sig]
 
You can also make your control into an Control-Array. All you have to do is set your Control's INDEX property to zero, then copy and paste the control and it will increment the INDEX property for you, thus creating a Control-Array. Then you can use a Loop to control the controls like this:

For intControl = 0 to 5
cmdInput(intControl).Enabled = True
Next intControl

However, this may not lend itself to be very maintence friendly since all the controls will have the same name, however the index will be what determines what control is accessed. If the the controls are grouped together or they all perform a similar function, then the control-array is probably the way to go.
[sig]<p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br> [/sig]
 
Just a small note; no need to hardcode 0 to 5 - you can still do this the object way:

Dim objControl as CommandButton
' assuming that cmdCommandButton is the name of your button array
For Each objControl In Me.cmdCommandButton
objControl.Visible = False
' or if you want to toggle the visible property use:
objControl.Visible = Not objControl.Visible
Next
[sig][/sig]
 
Thanks Gentlemen:
With all this data I should be able to work somthing out. I knew that it was not as simple as Numbering the objects and putting them in a loop. Rather bothersome to have to write code for each too.
73 RSH [sig][/sig]
 
When I have several controls I need to make visible/invisible, I put them into a frame and then set the frame to visible/invisible. This works great when the controls are placed more or less together on the form.

jim [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top