Problems with closeField

JonathanC at JonathanC at
Tue Sep 7 05:41:17 EDT 2004

I can't believe I'm having a problem with something as simple as 
"closeField"! :-)

I have a field which works like the Address bar of Internet Explorer. When 
you start typing into it, it tries to finish the word or phrase, according 
to a list in a separate field (which will eventually be hidden). If you 
end up typing a NEW word or phrase, you are asked if you would like this 
added to the list, for next time. I got it to work fine in HyperCard, but 
I'm stumped by the strange behaviour of "closeField".

Here are the essential scripts (watch for wrapped lines):

on openCard
  global autoCompleteList
  put fld "Auto-complete list" into autoCompleteList
end openCard

on keydown k
  global autoCompleteList
  if autoCompleteList = "" then pass keyDown
  if " " & charToNum(k) & " " is in " 3 8 9 13 28 29 30 31 " then pass 
  put k into selection
  put me into what
  if what="" then pass keydown
  get offset(return & what,return & autoCompleteList)
  if it>0 then
    put the number of lines in (char 1 to it of autoCompleteList) into 
    put line thisline of autoCompleteList into me
    select char length(what)+1 to length(me) of me
  end if
end keydown

on closeField 
  global autoCompleteList
  put me into temp
  if temp <> "" and not (temp is among the lines of autoCompleteList) then
    answer "Add" && quote & temp & quote && "to auto-complete list?" \
        with "Cancel" or "OK"
    if it = "OK" then
      if last char of autoCompleteList <> return and the length \
          of autoCompleteList > 0 then put return before temp
      put temp after autoCompleteList
      put autoCompleteList into fld "Auto-complete list"
    end if
  end if
  pass closeField
end closeField

Here's an explanation of the problem(s):
The "Auto-complete" field contains a closeField handler, which checks its 
contents against another field ("Auto-complete list") and offers to add a 
new line, if required.
However, the closeField handler seems to only be triggered when the field 
Expected behaviour (i.e. closeField triggered when new text is entered ... 
or the field is emptied) returns when the keydown handler is removed from 
the script of field "auto-complete" (or commented out).
Then a NEW problem occurs: If you enter a word/phrase that doesn't appear 
in the "Auto-complete list" field and then click out of the field, an 
answer dialog appears:
    Add <new text> to auto-complete list?
If you click "Cancel", the focus returns to the "Auto-complete" field. 
Then, if you click out of the field again, the whole thing starts again! 
And so on, until you click "OK". 
This is weird: Why does closeField not get triggered when (I think) it 
should, but then get triggered when (I think) it shouldn't?
Passing or not passing "closeField" makes no difference.

I have uploaded the stack to (4K 


Jonathan Cooper
Manager of Information / Website
Art Gallery of New South Wales
Sydney, Australia

More information about the use-livecode mailing list