Changing the text property of field controls from script

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Ian.Rowley
Posts: 7
Joined: Mon May 09, 2011 4:59 pm

Changing the text property of field controls from script

Post by Ian.Rowley » Wed Jul 27, 2011 11:09 am

Hoping someone can give me a point in the right direction. :D

I’m attempting to write some coding for labelling Function keys (F1-F20); naturally enough, the functions of the Function keys change, so the labelling needs to as well. Presently I have 20 “field” controls designated “F1lbl” to “F20lbl”. The intention is that each time I call up a new card, I post the text for the fields into a one-dimensional array called gLabelName; ‘X’ indicates that a Function key is unassigned.

The second part of the code sets up a loop (1-20) [myCount] and examines the contents of gLabelName. I’m using the concatenation function to create each of the field names in sequence, e.g. F5lbl. If the entry is “X”, the code should hide the field altogether, otherwise the code should set the text property of the field F[myCount]lbl to the corresponding string in the array, e.g. gLabelName[myCount]. In case it’s of relevance, the field controls are part of a control group associated with the stack.

I wonder whether part of the problem maybe to do with the fact that the field name (e.g. F5lbl, held in myInstance) isn’t enclosed in double quotes “ ” , but I can’t see anyway of concatenating a string with quotes. Current code is as follows:

global gLabelName

On preOpen Card

-- set up label array
put "Front TI,Fwd Left,Rear Left,X,X,Dismounted Exit,Fwd Right,Rear Right,Rear TI,X,All IR Off,X,Up,Alerts,X,Happy Days,X,Ack,Labels,X" into gLabelName
split gLabelName by comma

-- display labels/hide unused fields
repeat with myCount = 1 to 20
put "F" & myCount & "lbl" into myInstance
if gLabelName[myCount] is "X" then
set the visible of field myInstance to false
else set text of field myInstance to gLabelName[myCount]
set the visible of field myInstance to true
end repeat

End preOpenCard

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing the text property of field controls from script

Post by Klaus » Wed Jul 27, 2011 12:02 pm

Hi Ian,

your concatenating is OK!
What exactly does not work or work as exspected?


Best

Klaus

Ian.Rowley
Posts: 7
Joined: Mon May 09, 2011 4:59 pm

Re: Changing the text property of field controls from script

Post by Ian.Rowley » Wed Jul 27, 2011 1:27 pm

Hi Klaus,

Interesting, as I had assumed my coding was at fault! When I run the card/script, the unwanted field controls (i.e. designated by 'X') are not hidden, and the text of the field controls that are shown is not updated in accordance with what's in the array.

So my generic coding -
set text of field myInstance to gLabelName[myCount]

should work as well as the specific instance, say of
set text of field "F2lbl" to "Fwd Left"

? I'm still wondering whether the fact that I would use " " in the latter, is needed in the former where I'm using variables.

Many thanks
Ian

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing the text property of field controls from script

Post by Klaus » Wed Jul 27, 2011 1:58 pm

Hi Ian,

when supplying "bare" strings you must use quotes, especially when using more than one word in the string -> SPACE(s)!
but the engine will throw them away before using them. They are "just" used to let the engine differ them from possible variable names!

If you concatenate something like in your example the resulting variable will contain a string, the engine will recognize this fact and all is well 8)

Ah, and welcome to the forum! :D


Best

Klaus

Here some very good learning resources!
http://www.runrev.com/developers/lesson ... nferences/

Ian.Rowley
Posts: 7
Joined: Mon May 09, 2011 4:59 pm

Re: Changing the text property of field controls from script

Post by Ian.Rowley » Wed Jul 27, 2011 2:07 pm

Thanks Klaus,
I'll be sure to check out your suggestions for resources too.
I've inherited someone else's code so problem could lie there. Even if not, you've spurred a few additional thinks to try.
Nice to be welcomed into the Forum, and especially appreciate the quick turn around in responding.
Cheers
Ian :D

Ian.Rowley
Posts: 7
Joined: Mon May 09, 2011 4:59 pm

Re: Changing the text property of field controls from script

Post by Ian.Rowley » Thu Jul 28, 2011 9:44 am

Hello Klaus ... hello World!

I've managed to get most of the code working, with one exception! The script fails to make any array item in gLabelName equal to X invisible! From the best I can make out, the script is identifying when the array item = X, but then just ignoring the following line to make the field invisible. It should, it just doesn't. Is this something querky to Runrev set invisible/visible property when using a variable???

Just to be sure, I've highlighted the troublesome (non-functioning) line of text below:

Code: Select all

  -- display labels/hide unused fields
   repeat with myCount = 1 to 20
      put "F" & myCount & "lbl" into myInstance
      put gLabelName[myCount] into msg box
      
      if gLabelName[myCount] = "X" then 
      set the visible of field myInstance to false -- ****** this line of script doesn't work! ******
      else set text of field myInstance to gLabelName[myCount] 
      set the visible of field myInstance to true
   end repeat
If you could put me out of my misery, I'd be eternally grateful.

Many thanks
Ian

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing the text property of field controls from script

Post by Klaus » Thu Jul 28, 2011 11:35 am

Hi Ian,

I am not sure, but try a more readable "if then" form:
...

Code: Select all

 if gLabelName[myCount] = "X" then 
      set the visible of field myInstance to false -- ****** this line of script doesn't work! ******
   else 
     set text of field myInstance to gLabelName[myCount] 
     set the visible of field myInstance to true
end if
...
I'm never sure if other forms, e.g. without an "end if" like your one, are executed as exspected 8)
At least worth a try!

AH! WAIT! CleanGlasses... EDIT!
...
put gLabelName[myCount] into msg box
...
Might not work!
Just use:
...
put gLabelName[myCount]
## To show that value in the message box!
## You only need to address the message box by name if your are concatenating the output there like:
## put "CR & "Another line..." after MSG
## without BOX!
...


Best

Klaus

Ian.Rowley
Posts: 7
Joined: Mon May 09, 2011 4:59 pm

Re: Changing the text property of field controls from script

Post by Ian.Rowley » Thu Jul 28, 2011 1:33 pm

Hi Klaus, :-)

I'll keep your comment in mind, but I've managed to get the set text of field myInstance to gLabelName to work now!

My problem springs from this line:

Code: Select all

 
set the visible of field myInstance to false 
Which comes in the line immediately after the IF line statement. It just refuses to execute!

I had assumed that the variable myInstance (e.g. "F5lbl") wasn't the issue, as I use almost identical code to set the visible of fields with text (i.e. line immediately after ELSE line) further on in the script. In fact, I've tried it out in a stand alone stack elsewhere to make all the fields invisible, and it seems to work fine. So I'm at a loss to fathom why it won't work here! Plus, LiveCode doesn't generate any error messages (script or execution)!

Very puzzling!

Appreciate any insights. Or indeed thoughts on any alternative way to make fields invisible using a variable reference.

Many thanks for your time.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing the text property of field controls from script

Post by Klaus » Thu Jul 28, 2011 2:31 pm

Hi Ian,

hmmm, very strange.

I just can recommend to debug and check the contents like this:

Code: Select all

...
put gLabelName[myCount] into tCurrentvalue
BREAKPOINT
## Check tCurrentvalue! 
## I guess it is never X or there is an additonal SPACE or whatever!?
if tCurrentvalue = "X" then 
      set the visible of field myInstance to false -- ****** this line of script doesn't work! ******
   else 
     set text of field myInstance to gLabelName[myCount] 
     set the visible of field myInstance to true
end if
...
Best

Klaus

Ian.Rowley
Posts: 7
Joined: Mon May 09, 2011 4:59 pm

Hacked Solution

Post by Ian.Rowley » Fri Jul 29, 2011 8:46 am

Morning Klaus,

I thought I'd post my final solution, just in case some other poor deluded soul ever comes to this thread looking for a solution to their problem!
I still have no idea why the line of code following if gLabelName [myCount] is equal to "X" doesn't work (!!!), so I've side-stepped the problem entirely!
I've added some initial code to make ALL label "lbl" fields invisible.
For the IF...THEN statement I've altered it to read if gLabeName [myCount] <> "X" then add contents of array item to text of "lbl" field, and make visible, making the need for an alternative condition redundant (i.e. as the "lbl" field will already be set to invisible by the initial section of code).

Code: Select all

global gLabelName

On preOpenCard
   
   ## set up label array
   put "Front TI,Fwd Left,Rear Left,X,X,Dismounted Exit,Fwd Right,Rear Right,Rear TI,X,All IR Off,X,Up,Alerts,X,Happy Days,X,Ack,Labels,X" into gLabelName
   split gLabelName by comma
   
   ## make all label fields invisible
   repeat with myCount = 1 to 15
      put "F" & myCount & "lbl" into myInstance
      set the visible of field myInstance to false
   end repeat
   
   ## put text from gLabelName into field [myCount] and make label visible/leave labels containing X invisible
   repeat with myCount = 1 to 15
      put "F" & myCount & "lbl" into myInstance
      if gLabelName[myCount] <> "X" then
         set text of field myInstance to gLabelName[myCount] ## else statement deleted
      set the visible of field myInstance to true
     else ## do nothing
     end if
  end repeat

End preOpenCard
Interestingly, LiveCode let me add the END IF statement this time. Previously LiveCode had thrown up a script error whenever I'd included it (in this instance) as it seemed to be conflicting with the END REPEAT statement (again, I'm not sure why, as they were suitably nested).

Anyhow, just to repeat, many thanks for your time and patience in helping me through this one.

Cheers - Ian :D

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing the text property of field controls from script

Post by Klaus » Fri Jul 29, 2011 11:27 am

Hi Ian,

glad you got it working!

Hint:
If there is no "else" case you can omit the "else":

Code: Select all

...
   if gLabelName[myCount] <> "X" then
      set text of field myInstance to gLabelName[myCount]
      set the visible of field myInstance to true
     end if
...
Best

Klaus

Post Reply