Setting Text Style Based on Word in Field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
J316
Posts: 6
Joined: Wed Oct 28, 2015 11:04 am

Setting Text Style Based on Word in Field

Post by J316 » Sun Dec 06, 2015 10:39 pm

Hi Folks,

Having trouble with this as I'm just getting started in LC.
With a field named month listing the following:

Month 1
2nd
4th
etc.
Month 2
12th
17th
etc.

I am wanting to set the textStyle to link for all lines that contain the word "Month".
Have been modifying the below script without success.

on mouseUp
put field "month" into tmonth
repeat with x = 1 to the number of lines in tmonth
if line 1 contains "Month" then
set the textStyle of it to link
put tTest22 into field "test22"
end if
end repeat
end mouseUp

Any help much appreciated!

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

Re: Setting Text Style Based on Word in Field

Post by Klaus » Sun Dec 06, 2015 11:23 pm

Hi J316,

1. welcome to the forum! :D

2. This should do the trick, please note my comments!

Code: Select all

on mouseUp
   put field "month" into tMonth
   
   ## To speed up things when working with fields, we lock the screen!
   ## Since fields are quite complex to draw, we might see LC working line by line otherwise :-D
   lock screen
   repeat with x = 1 to the number of lines of tmonth
      ## if line 1 contains "Month" then
      ## You want to check ALL lines and not only line 1, right?

      ## Looks like all the lines START with Month, so I used ...BEGINS WITH... here, if not chage back to ...CONTAINS...
      if line i of tMonth begins with "Month" then
         
         ## set the textStyle of it to link
         ## IT is not used anywhere in this handler so it may contain 
         ## ANYTHING (from an earlier command/functoin) or NOTHING!
         ## We change the according line in the actual field:
         set the textstyle of line i of fld "Month" to "link"
         
         ## tTest222 is also not defined in this handler so you will see the string tTest 22 in fld "test22"
         ## put tTest22 into field "test22"
      end if
   end repeat
   
   ## Done!
   unlock screen
end mouseUp
3. Please take a look at these stacks to get more of the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

Best

Klaus

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Setting Text Style Based on Word in Field

Post by quailcreek » Sun Dec 06, 2015 11:27 pm

Try this.

Code: Select all

on mouseUp
   put field "month" into tmonth
   repeat with x = 1 to the number of lines in tmonth
      if line x of tmonth contains "Month" then
         set the textStyle of line x of tmonth to link
         put tTest22 into field "test22"
      end if
   end repeat
end mouseUp
Tom
MacBook Pro OS Mojave 10.14

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

Re: Setting Text Style Based on Word in Field

Post by Klaus » Sun Dec 06, 2015 11:34 pm

Hi Tom,

...
set the textStyle of line x of tmonth to link
...
"tMonth" is a variable! 8)


Best

Klaus

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Setting Text Style Based on Word in Field

Post by quailcreek » Mon Dec 07, 2015 12:15 am

OOps.

Code: Select all

 set the textStyle of line x of fld "month" to link
Tom
MacBook Pro OS Mojave 10.14

J316
Posts: 6
Joined: Wed Oct 28, 2015 11:04 am

Re: Setting Text Style Based on Word in Field

Post by J316 » Mon Dec 07, 2015 12:41 am

Hey again Klaus,

I guess I'm a little confused about the lines in fields from your comment about All of them starting with Month. My assumption was for the
below example that only lines 1 and 5 begin with Month...as they would appear in the contents of a field?? I am trying to have "Month 1" and "Month 2" only to become links.

Month 1
2nd
4th
etc.
Month 2
12th
17th
etc.

I did try the script with "starts with" and "contains", no quotes, of course. It returned an error both ways:
execution error at line 12 (Chunk: error in range start expression),char 1
This is the line in the script: if line i of tMonth contains "Month" then

Thanks much for the help. I know this is elementary for you folks, but I will get better...promise!

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

Re: Setting Text Style Based on Word in Field

Post by Klaus » Mon Dec 07, 2015 2:07 pm

Hi J316,

sorry, I made a little typo in my script, lets see if you can spot it, too:

Code: Select all

...
 repeat with x = 1 to the number of lines of tmonth
      if line i of tMonth begins with "Month" then
      ...
       set the textstyle of line i of fld "Month" to "link"
...
Best

Klaus

J316
Posts: 6
Joined: Wed Oct 28, 2015 11:04 am

Re: Setting Text Style Based on Word in Field

Post by J316 » Mon Dec 07, 2015 8:29 pm

Ah! Klaus,

Thanks for the little challenge...I do appreciate it. Sorry about the delay...my day job got in the way! I changed the i's to x's since the initial repeat was defined that way. I am also assuming, and will try i for this also, it is like a variable and can be most anything one would like it to be as long as one is consistent in the script.?

I also removed the quotes from:
set the textstyle of line x of fld "Month" to "link"
making it:
set the textstyle of line x of fld "Month" to link --I suppose this is optional?

All worked fine and I really appreciate it.

Klaus...this wasn't actually my first post. I saw some folks viewed my first one, but did not respond. I assumed I just ask a stupid question or maybe it wasn't posted in the correct place in the forum. Would you mind looking below at my first post and tell me if it's a stupid question, in the wrong place or, if you will, address it. I really wanted to make a decision on the below item before I got too far along in my efforts. Thanks again, very much! And here it is:

"Hi Folks,

This is my first posting a question here and thanks for all you are doing to help.

I am reading about retrieving text files that are located on a server, but am not sure what the most efficient way to proceed might be. My app is almost exclusively dealing with text. I have created several fields for displaying the text stored in the field contents, but I'm concerned that with the volume of text I will ultimately have, it may slow down the app. I am not interested (at this point) in writing to the server. In reading, I saw a suggestion of using the "load" command as being asynchronous and also being able to dump the cache. But I also wondered if using FTP would be as good a choice. I'm just too new to this stuff (about a month in) to trust my own instincts.

I have a text file stored on my web server, but forum says I can't post web links. Need help with that for future I suppose?

Might you point me in the direction you think is best for me to proceed? Thanks upfront for any help and again for all you do!

J316"

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

Re: Setting Text Style Based on Word in Field

Post by Klaus » Mon Dec 07, 2015 11:43 pm

Hi J316,
J316 wrote:Ah! Klaus,

Thanks for the little challenge...I do appreciate it. Sorry about the delay...my day job got in the way! I changed the i's to x's since the initial repeat was defined that way. I am also assuming, and will try i for this also, it is like a variable and can be most anything one would like it to be as long as one is consistent in the script.?
it actually IS a variable, a temoprary one, but a real and valid variable! :D
J316 wrote: I also removed the quotes from:
set the textstyle of line x of fld "Month" to "link"
making it:
set the textstyle of line x of fld "Month" to link --I suppose this is optional?
No, that is correct syntax with quotes!
If in doubt, always take a look into the dictionary! 8)

Your first posting can be found here:
http://forums.livecode.com/viewtopic.ph ... 43#p135343
And it looks that someone just answered your question!


Best

Klaus

J316
Posts: 6
Joined: Wed Oct 28, 2015 11:04 am

Re: Setting Text Style Based on Word in Field

Post by J316 » Tue Dec 08, 2015 12:10 am

Thanks again, Klaus. Will do!

Post Reply