We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Referring to controls with variables (30 pts)

The_Oracle
The_Oracle asked
on
Medium Priority
180 Views
Last Modified: 2010-05-03
How can I refer to controls with a variable.E.g. I want to set the 'left' property of six text boxes to 100. Rather than type "text1.text = 100" for each (changing the number), how can I use one line in a repeat loop, where the number '1' in the above example is a variable that changes?
Comment
Watch Question

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
Make a control array of text boxes:
So name all the text boxes "MyText" and set the index property to 0,1,2,3,4,5 etc
Now use a for loop
--
Dim t as textbox
for each t in MyText
   t.text = 100
next
--
or
--
for i = 0 to 6
   MyText(i).text = 100
next

Commented:
Oops...made a mistake. That loop should start out with:

For i = 0 To (Form1.Controls.Count - 1)

Commented:
Dim objControl As Control

  For Each objControl In Me
    If TypeOf objControl Is TextBox Then
   
     objControl.Left = 100
     
    End If
  Next

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.