Page 1 of 1

Newbie control question

Posted: Fri Apr 27, 2007 5:44 am
by kpeters
How do I determine the control type? I want to clear all fields on a card.



repeat with idx = 1 to number of controls in this card

if control idx <is a field> then put empty into control idx

end repeat

TIA,

Kai

Re: Newbie control question

Posted: Fri Apr 27, 2007 8:21 am
by marielle
To answer the original question.

Code: Select all

put word 1 of the long name of control idx into tControlType
What you seem to be trying to achieve:

Code: Select all

put the long id of this card into tContainer
put the number of fields in tContainer into tFldQty
lock screen
repeat with x = 1 to tFldQty
  set the text of field x of tContainer to empty
end repeat
unlock screen
lock and unlock screen are there to greatly speed up execution time (to consider as soon as you have more than 5 fields to process).

To restrict this to a given group, change the first line to 'the long id of group "myGroup" of card "myCard"'


If you were trying to delete all fields, you would rather use this

Code: Select all

put the long id of this card into tContainer
put the number of fields in tContainer into tFldQty
repeat with x =  tFldQty down to 1
    delete field x of tContainer
end repeat
Add lock/unlock screen to prevent the view from changing all the time, which can be disconcerting for the user.

The reason the count goes down to 1 is because once you have deleted the first field, the following ones get renumbered from 1 to tQty-1. If you were looping from 1 to tQty, by the time you would reach tQty, a field of number tQty wouldn't exist anymore.

Re: Newbie control question

Posted: Fri Apr 27, 2007 8:58 am
by Mark
kpeters wrote:How do I determine the control type? I want to clear all fields on a card.

repeat with idx = 1 to number of controls in this card

if control idx <is a field> then put empty into control idx

end repeat

TIA,

Kai
Kai,

Do you want to delete fields or erase their contents (I believe you want to erase their contents, but Marielle provided code to delete fields):

Code: Select all

-- delete fields
repeat (number of fields)
  delete fld 1
end repeat


-- erase contents
repeat with x=1 to number of fields
  put empty into fld x
end repeat
Best,

Mark

Posted: Fri Apr 27, 2007 7:08 pm
by kpeters
Thanks Marielle & Mark - that more than answers my question.

Marielle - why do you use 'helper' variables in your code sample like
tFldQty?

Is it because the compiler (when building standalones) is not optimizing well, i.e. it would evaluate the expression "number of fields in tContainer"

Code: Select all


-- put the number of fields in tContainer into tFldQty 
lock screen 
repeat with x = 1 to number of fields in tContainer
upon every repeat cycle?

TIA,
Kai

Posted: Fri Apr 27, 2007 7:26 pm
by kpeters
Oops - and one real problem:

Your code also clears the labels on my card - I just want to erase the content of fields....

Kai

Posted: Fri Apr 27, 2007 7:31 pm
by marielle
kpeters wrote:Marielle - why do you use 'helper' variables in your code sample like tFldQty?

Is it because the compiler (when building standalones) is not optimizing well, i.e. it would evaluate the expression "number of fields in tContainer"
Sometimes, there are problems and the string must be put in between parentheses. But that's not really the reason. The main reason is code legibility and ease of maintenance and debugging.

In contrast to what is common, I do a lot of code reuse. The code I gave would in fact be a function. Because I reuse, I appreciate to rapidly understand what the code does. To use meaningful labels greatly helps with that. If you take "repeat with x = 1 to number of fields in tContainer" is long to read. Exceeds 7 words, and as such taps onto the attentional resources. The option option, "repeat with x = 1 to tFldQty" is comparatively easier to process (cognitively speaking) and greatly diminishes the opportunity for misunderstanding what the code does.

I also prefer to keep elements that belong to different logics separate. If you get to change the format from number of fields to number of items (comma separated) or number of lines in a list (cr separated), like in Mark's alternative, the logic of the repeat loop doesn't change. How you obtain the value for tFldQty does.

All down to personal preferences, really. I have a logical mind, I like abstraction. Other persons prefer to express their thoughts with real language. Simply go for what works for you.

Posted: Fri Apr 27, 2007 9:51 pm
by Mark
Hi Kai,

If you want to erase the text of all editable fields, but not the label fields, you could run the following script:

Code: Select all

-- erase contents
repeat with x=1 to number of fields
  if not the lockText of fld x then
    put empty into fld x
  end if
end repeat
but if you have editable fields that you don't want to be cleared, you need to give these fields a flag. You could use a custom property for instance. The script below uses a custom property named cEditable.

Code: Select all

-- alternative
repeat with x=1 to number of fields
  if not the cEditable of fld x then
    put empty into fld x
  end if
end repeat
Of course, it is not obligatory to use custom properties. You could also use the name of the field as a flag or even keep a list with field names.

Code: Select all

-- alternative 2
repeat with x=1 to number of fields
  if "Edit" is in the name of fld x then
    put empty into fld x
  end if
end repeat

-- alternative 2
put "Name,Address,Phone,Fun" into myList
repeat with x=1 to number of fields
  if the short name of fld x is in myList then
    put empty into fld x
  end if
end repeat
Best,

Mark

Posted: Fri Apr 27, 2007 10:36 pm
by marielle

Code: Select all

-- alternative 3
put "Name,Address,Phone,Fun" into myList
repeat for each item tName in myList
  set the text of field tName to empty
end repeat

Posted: Fri Apr 27, 2007 11:06 pm
by Mark
Yes, Marielle, you're right :-) I was thinking too much in line with the previous scripts.

Mark