Page 1 of 1

If...then...else or switch

Posted: Fri Oct 23, 2009 1:43 pm
by mtecedor
Hi again, I keep working with the hiliting effect to see if I can make it work exactly the way I want it to.

I want users to be able to highlitgh a text using two colours, and then recover ony the highlighted words/sentences in the next card. In my previous attempts I was working with all the code in the hiliter buttons, but I was running in small problems.

Now, what I am trying is to have the hiliter effect on the hiliter buttons, and the "put in field output" in the "next card button. So, I tried with an if statement, but it doesnt work. I though I may need a switch statement, but I am not sure how it works, or if that is the appropriate way to get what I want in this case.

This is what I have so far:

on mouseUp
put field "Text2" into tText
repeat with i = 1 to the number of chars of tTest
if the backgroundColor is yellow
then
put i into yText
sort yText numeric
set the textStyle of yText to bold
put yText into field "output"
if the backgroundColor is green
then put i into gText
put i into gText
sort gText numeric
set the textStyle of gText to cursive
put gText into field "output"
end if
end if
end repeat

end mouseUp


As for now, this is not working, I though a swicth statement could work, the problem is that I dont understand very well how it works.

Thanks again

Marta

Posted: Fri Oct 23, 2009 5:10 pm
by Janschenkel
You cannot apply text styles to a variable - only to actual controls or chunks of a text field. Either work with the htmlText property of the field, inserting the necessary tags, or put the text into another field, and apply the text styling to that.

Jan Schenkel.

Posted: Sun Oct 25, 2009 2:28 pm
by mtecedor
Sorry for the insistence in this topic, but I don't know why my code is not working.

My scenario:
Users hilite words in a text in two colors (yellow and green).
Then, when they go to the next card, the text they have hilited appears in a field.

Since I cannot apply textStyle to a variable, I though that just telling revolution to check the backgroundcolor of each char would be enough.


In the go to "next" card button I have this code:

on mouseUp
repeat for each char in field "Text2"
if the backgroundColor of char is not empty then
put char into field "output"
end if
end repeat
end mouseUp

I get this error message:

button "Next": compilation error at line 2 (repeat: missing 'in'), char 25

It's just that what I am trying to accomplish is not possible or that I am doing a stupid mistake?

Marta

Posted: Sun Oct 25, 2009 3:18 pm
by Klaus
Hi Marta,

you forgot to use a variable :-)

Code: Select all

on mouseUp 

  ## Maybe you want to empty the field first
  ## before you output the chars into it?
  put empy into fld "output"
  ## OPTIONAL, see below

  repeat for each char TCHAR in field "Text2" 
    if the backgroundColor of char TCHAR is not empty then 
    ## put TCHAR into field "output" 
    ## The line above will always replace the current char in fld "output"
    ## so after the repeat loop only the LAST char will be in the field!
    ## Maybe you watn to append the chars? Just wondering...
    put TCHAR after fld "output"
    end if 
  end repeat 
end mouseUp


Best

Klaus

Posted: Sun Oct 25, 2009 7:07 pm
by bn
Marta,
unfortunately you are still a little vague on what exactly you are trying to achieve. Now you want to go to a new card, that is important for the layout and scripting of things. Is the field output on the same card or on the following card? Or do you have a group with the background behavior set to true?
From what I gather, the following would get you all the green and yellow text of field "text2" and put it into field output on the same card as the field "text2" and mark it green and yellow and sets it to bold and italic, each color on a new line.

Code: Select all

on mouseUp
   lock screen
   repeat with i = 1 to the number of chars of field "text2" 
      put the backgroundcolor of char i of field "text2" into tColor
      if tColor is "255,255,0" or tColor is "0,255,0" then
         if tColor is "255,255,0" then  -- this is yellow
            put char i of field "text2" after field "output"
            set the backgroundcolor of char i of field "output" to yellow
            set the textStyle of last char of field "output"  to bold 
         end if
         if tColor is "0,255,0" then -- this is green
            put char i of field "text2" after field "output"
            set the backgroundcolor of char i of field "output" to green
            set the textStyle of last char of field "output"  to italic 
         end if
      else
         put return after field "output"
      end if
   end repeat
   repeat with i = the number of lines of field "output" down to 1
      if line i of field "output" = "" then delete line i of field "output"
   end repeat
end mouseUp
from your example:
repeat for each char in field "Text2"
this is not the right syntax for repeat for each, please look that up in the dictionary.

the for each form of the repeat puts the literal of what you want into a variable, you dont know where it is or what backgroundcolor it has. You would need a counter to tell you that.
best would be to use a "repeat with i = 1 to the number of chars of field "text2" form, see above

regards
Bernd

Posted: Mon Oct 26, 2009 5:17 am
by mwieder
Marta-

Are you actually setting the backgroundcolor of the selectedtext or just changing the color of the text itself? If it's the latter, then the following would work in your Next Card button:

Code: Select all

on mouseUp
    local x
    
    repeat with x=1 to the number of chars of field "Text2"
        if the forecolor of char x of field "Text2" is not the forecolor of field "Text2" then
            put char x of field "Text2" after field "output" of next card
        end if
    end repeat
    go next
end mouseUp

Posted: Wed Oct 28, 2009 1:43 pm
by mtecedor
Thanks a lot to all, it woked!