Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is it possible to use a variable value in .ControlName?
Message
From
26/11/2002 07:10:02
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
26/11/2002 07:05:13
General information
Forum:
Visual FoxPro
Category:
Object Oriented Programming
Miscellaneous
Thread ID:
00726887
Message ID:
00726888
Views:
16
This message has been marked as the solution to the initial question of the thread.
>Hi all.
>I have a form with 47 fields
>I have a loop that performs some calculations 47 times and need to display the results in every one of the fields sequentially.
>Is there some way to substitute the "THISFORM.ObjectName" with a variable that increases +1 in each pass?
>My text fields are named Text1 through Text47.
>So I tried something like this (abreviated here):
>
>xCount=1
>DO WHILE xCount<48
>xText="Text"+(alltrim(str(xcount)))
>THISFORM.xText.value=xCount
>xCount=xCount+1
>ENDDO
>
>I got (obviously) an error message when reaching the THISFORM line about not finding the xText element.
>
>Is there a possible way around this or will I have to repeat the code for each one of the fields?
>TIA for your time
>
>Ulises

You can use macro substitution.
for FieldNum = 1 to 48
  FieldName = "Text" + trans(FieldNum)
  ThisForm.&FieldName..Value = FieldNum
next

* or:

for FieldNum = 1 to 48
  FieldName = "Text" + trans(FieldNum)
  Command = "ThisForm." + FieldName + ".Value = " + FieldNum
  &Command
next
Note also how the FOR saves you some coding in the loop. trans() requires VFP 6 or later (equivalent to alltrim(str()), if the argument is numeric)).

HTH, Hilmar.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform