rename field after insertion

Cubist at Cubist at
Tue Jul 13 03:28:55 CDT 2004

sez bob at
> I have a Textfield (TxA1) on a card in one stack (a template stack)  
> and I have a button on this card that inserts it into a card in 
> another stack "working stack". If I click on the button again it 
> inserts a Textfield again. OK it is supposed to do this.
> However, "both" the textfields are called TxA1. I want to name the 
> first field TxA1, but I want the second textfield to be called TxA2 
> and if there was a third click then the third field to be TxA3.
> So the button click on stack template would have to read textfields in 
> the working stack and if they exist, determine the largest number 
> after TxA in the name and insert a textfield with TxA? +1
> Any ideas on how this can be done?
   It's worth noting that all fields have a number, and whenever you create a 
field, including by copying/pasting or otherwise duplicating an existing 
field, the newly-created field will be the highest-numbered one. THis means you 
always know exactly which field is the newly-created one, and you can manipulate 
that puppy any which way you like. Here is one way you might do what you need:

on NextFieldPlease
  copy field "TxA1" to this card
  put 0 into Kounter
  repeat # this only *looks* like an infinite loop, honestly!
    add 1 to Kounter
    if there is not a field ("TxA" & Kounter) then exit repeat # see?
  end repeat
  set the name of field (the number of fields on this card) to ("TxA" & 
  #also, whatever other alterations you might want to inflict on this field
end repeat

   This code will count up thru the all the "TxA1", "TxA2, "TxA{n}" fields, 
and give the newly-created field the lowest-numbered "TxA{n}" name that *isn't* 
currently in use. If this isn't what you want, here's another possibility for 
the loop:

  put 0 into HFN # HighestFieldNumber
  repeat with K1 = 1 to (the number of fields on this card) - 1
    put the short name of field K1 into Fred
    if char 1 to 3 of Fred <> "TxA" then next repeat # this field's name is 
    delete char 1 to 3 of Fred
    if Fred is not an integer then next repeat # another wrong name
    put max (Fred, HFN) into HFN
  end repeat
  set the name of field (the number of fields on this card) to ("TxA" & HFN + 

   This one will automatically assign a "TxA{n}" name which is exactly 1 
higher than the highest previously-existing "TxA{n}" name.

   Hope this helps...

More information about the use-livecode mailing list