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

Can i use for loop here? or array? 2

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
585
PH
Hi everyone.... may i just ask if i could use array or for to shorten these commands? If so, please show me how... with a little explanation to help me understand.... Thanks and God bless....

PROCEDURE text19.click()

IF this.parent.text19.value = 1

this.parent.text6.value = 1
this.parent.text7.value = 1
this.parent.text8.value = 1
this.parent.text9.value = 1
this.parent.text10.value = 1
this.parent.text11.value = 1
this.parent.text12.value = 1
this.parent.text13.value = 1
this.parent.text14.value = 1
this.parent.text15.value = 1
this.parent.text16.value = 1
this.parent.text17.value = 1
this.parent.text18.value = 1
ELSE

this.parent.text6.value = 0
this.parent.text7.value = 0
this.parent.text8.value = 0
this.parent.text9.value = 0
this.parent.text10.value = 0
this.parent.text11.value = 0
this.parent.text12.value = 0
this.parent.text13.value = 0
this.parent.text14.value = 0
this.parent.text15.value = 0
this.parent.text16.value = 0
this.parent.text17.value = 0
this.parent.text18.value = 0
this.parent.text19.value = 0
thisform.Refresh()

ENDIF
 
think this will do it... but untested.

in .text19's click event...
Code:
for i=6 to 18
     m.lcCtrl = "this.parent.text"+transform(m.i)+".value"
    &lcctrl = this.value
next
this.parent.refresh()


hth

n
 
I was going to suggest:

Code:
** loop for the items required
FOR I=6 TO 18
	** assign the control to a variable (because ExecScript() cannot use the THIS.PARENT reference)
	m.OBJ = EVALUATE(THIS.PARENT.TEXT"+TRANSFORM(i))
	** test for the TEXT19 value and assign as appropriate
	IF THIS.PARENT.TEXT19.VALUE = 1
		EXECSCRIPT("m.OBJ.VALUE=1")
	ELSE
		EXECSCRIPT("m.OBJ.VALUE=0")
	ENDIF
NEXT
THIS.PARENT.REFRESH()

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
A small point: You don't need THISFORM.Refresh here - unless the form also has fields that are bound to a control source and which are also being updated. There's no harm in doing it anyway - except for the small amount of time it will take - but it is not at all necessary.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi

I guess you meant "THIS.Value" instead of "THIS.PARENT.Text19.Value" since you clicked the Text19 control - unless I miss something.

Furthermore it is not good practice to change the value of the control that initiates the change of the other controls' value. You may want to add a button control to change the values of the textboxes

Code:
PROCEDURE text19.click()

[s]IF this.parent.text19.value = 1[/s]

[highlight #FCE94F]IF This.Value = 1[/highlight]

this.parent.text6.value = 1
this.parent.text7.value = 1
this.parent.text8.value = 1
this.parent.text9.value = 1
this.parent.text10.value = 1
this.parent.text11.value = 1
this.parent.text12.value = 1
this.parent.text13.value = 1
this.parent.text14.value = 1
this.parent.text15.value = 1
this.parent.text16.value = 1
this.parent.text17.value = 1
this.parent.text18.value = 1

ELSE

this.parent.text6.value = 0
this.parent.text7.value = 0
this.parent.text8.value = 0
this.parent.text9.value = 0
this.parent.text10.value = 0
this.parent.text11.value = 0
this.parent.text12.value = 0
this.parent.text13.value = 0
this.parent.text14.value = 0
this.parent.text15.value = 0
this.parent.text16.value = 0
this.parent.text17.value = 0
this.parent.text18.value = 0
[highlight #FCE94F][s]this.parent.text19.value = 0[/s]
[s]thisform.Refresh()[/s][/highlight]

ENDIF 

THISFORM.Refresh()

For replacing the TextBox Values it's up to you the choose from the suggestions.

hth

MarK
 
Just want to point out that every container control has an Objects collection that you can loop through. Might not be the right choice here, but worth considering.

Tamar
 
Thank you everyone for all your answers... i tried using Nigel.... and it worked fine.... I loveprogramming.... its challenging...but what is the meaning of m.lcCtrl & &lcctrl...please i want to learn....Thanks again.....God bless....
 
Ok Nigel... I'll try to study and learn macro.... Thanks again.... God bless....
 
m.lcCtrl means the variable named lcCtrl. In VFP, fields always have priority, so if there happened to be a field named lcCtrl in the table open in the current work area, then its value would be used when you write just lcCtrl. By putting m. in front, you're telling VFP to go right to the variable without checking to see whether there's a field with that name. You should _always_ use m. when you reference variables (except for a very few places where you can't put fields).

I wrote about this topic here:
Tamar
 
I have to admit that I am guilty of this myself, that is, of being a bit lackadaisical in my use of m.

That said, one thing that irks me (perhaps irrationally) is using m. on the left of an assignment:

Code:
REPLACE Customer WITH m.CustName

is good. But:

Code:
m.CustName = "Mike"

is not so good. Similarly:

Code:
FOR m.lnI = 1 TO FCOUNT()

There's nothing particularly wrong with those last two. They will work exactly the same if you omit the m., and I doubt if there is any difference in performance. But I still find it annoying.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike,

I'm not following you, what's wrong with m.CustName = "Mike"?

I can maybe see there is repetition and 'inelegance' in FOR m.lnI = 1 to FCOUNT()

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Griff, there's nothing really wrong with it. It's just that the m. is redundant here. It's not needed to distinguish a variable from a field name, since a field name cannot appear to the left of an assignment.

This is just me being pernickity. I also find it irksome when I see things like "close proximity" or "a short summary" or "predictions of the future".

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ah, I get you, you can't do myTable.CustName = "Mike"

You can have a table open with a field called custname, and call CustName="Mike", in which case VFP makes a new variable called m.CustName and assigns "Mike" to it.
The underlying table is unchanged - could cause confusion to a novice.

Off topic, but I loathe 'It's 30 times smaller', from my father who always railed at that!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Hello,

I use m. now, I remember a thor tool do do this.

Anyway :
in our lazy days we did not use m. but use (up to now) a strict naming convention, no field may begin with lc/ln/ld/lt/obj_,....

Regards
tom

I would do the above like this :

Local lnnew, lni, lcctrl
m.lnnew = 0
IF This.Value = 1
m.lnnew = 1
endif
for lni=6 to 18
m.lcCtrl = "this.parent.text"+transform(m.lni)+".value"
&lcctrl = m.lnnew
next




 
Mike said:
Tom, this is no criticism of your code, but - and this is my point - why do you use m. in m.lnnew = 0 but not in for lni=6 to 18?

I'll assume that this is not real code, just something that Tom wrote in a hurry in the TekTips editor.

Writing in the VFP editor, enhanced by Thor Intelissense, Tom would probably write this way:

Captura_de_ecr%C3%A3_2021-03-23_111718_z9ts6r.png
 
Hello again,

sorry, you are right, written in a hurry...

(Our cats think I spend too much time in homeoffice with work :),
and nobody should try to discuss with a norwegian forestcat/british shorthair mix. kitten@6.5 kgs)

Regards
tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top