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

Macro Substitution 1

Status
Not open for further replies.

Wehutexy

Technical User
Jul 23, 2021
1
IN
I have 2 text boxes named text1 and text2 in my form and one command button
On the click event of the button I wrote this code......

For i=1 to 2 step 1
Thisform.text(i).Value="ABC"
End for


This is give error in 2nd line. I want to use textbox name with programmatically.
 
Brackets are not possible here, they can surround name expressions, with limitations.

What can be used anywhere is macro substitution. Not with i as numeric variable, though. You need a string variable with the missing part of code or a name. You then can use &variablename. with the dot ending the macro substitution in this case.

Code:
For i=1 to 2 && step 1 is the default and not necessary
   cNumber = transform(i)
   Thisform.text&cNumber..Value="ABC"
End for
Only using one dot here would end macro substitution, but expand it to Thisform.text1Value. Therefore you need a double dot here.

Chriss
 
Welcome to the forum, Wehutexy. Chris has given you a good answer, and I can't add anything to it.

It would help everyone if, the next time you post a question, you give it a meaningful title. This helps forum members when looking for questions they might be able to answer, and it also helps people looking for a solution for the same or similar problems.

Some examples of helpful titles: "Extracting addresses from UK.gov price paid data" and "How to enter number in a textbox in calculator style?". But "Visual FoxPro" is not a helpful title as it could apply to just about every thread in the forum.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike,

And Wehutexy; if an instructor gave you that task, it was a perfect task to force you into finding out about macro substitution and learn it with all its details. Very often you can use macro substitution without the dot as end delimiter for the substitution variable name, as any other character not valid for a variable name also indicates the end of it. If there's a dot though, VFP always assumes it's the macro substitution variable name end delimiter and not that it should remain for the rest of code, ie here to have .Value. So in this special case you need to double the dot as one of them is gulped by macro substitution.

And one more thing: Even for name expressions the variable or in general the name expression in the brackets must be a string, not a number. So a numeric i will never work.
You can't use name expressions anywhere like macro substitution, though. So even when you have a name of a property like Caption of a window or label, you can't use it for a name expression. So this does NOT work, for example:

Code:
lcObjectname = "Caption"
_screen.(lcObjectname) = "This is the FoxPro window"

Chriss
 
Wehutexy,

Mike & Chriss gave you the best advice. Just to add to your "macro" knowledge, here's another way (not necessarily the best) that would also work:

Code:
FOR ii = 1 TO 2
 objProp = 'THISFORM.Text' + TRANS(m.ii) + '.Value'
 &objProp = 'ABC'
ENDFOR

Steve
 
yet another way which would be more useful if you are setting multiple properties...

Code:
FOR i=1 TO 2
	loTxt = EVALUATE('THISFORM.text'+TRANSFORM(m.i))
	IF TYPE('loTxt') = 'O'
		loTxt.VALUE = 'ABC'
		loTxt.ENABLED = .F.
		loTxt.fontbold = .t.
	ENDIF
NEXT


hth

n
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top