Problems using formmatedtext in a field on another card

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
IanMcK
Posts: 16
Joined: Mon Jul 05, 2010 10:47 am

Problems using formmatedtext in a field on another card

Post by IanMcK » Sat Feb 25, 2017 3:03 pm

Hi

Does the formattedText only work in a field on the current card? Is it not possible to retieve the formattedText from a field on another card and modify the text accordingly before going to that card?

I have multiline text over 5 lines which I place in a field on another card before going to the card.
the code is

Code: Select all

-- tWord contains FLAMENCO DANCER for example
-- item 2 of tWordLine contains 4
-- kCardCount is the loop variable 1 to 4

put cr& tWord &cr&cr&cr& item 2 of tWordLine into tWordForDisplay

put "playCardFld_"& kCardCount into tPlayCardName

put tWordForDisplay into fld tPlayCardName of card "playCard"

Now this example will wrap onto 6 lines so I want to remove one of the 3 blank lines to keep the line count at 5.
This is the code I used

Code: Select all

-- check formattedtext to see if multiLine
      
put the number of lines in the formattedText of fld tPlayCardName of card "playCard" into tNumberOfFormattedLines 
      
-- should be 5 - blank, word, blank, blank, blank, number
  
if tNumberOfFormattedLines > 5
      
then -- take out a blank line
         
replace cr&cr&cr with cr&cr in fld tPlayCardName of card "playCard" 
      
end if
Problem is it doesn't work!
FLAMENCO DANCER is still on one line. The numberofFormattedLines is 5 even though it has been retrieved from the text field on the next card. When the stack goes to the next card FLAMENCO DANCER is shown on 2 lines, the numberOfFormattedLines is 6 and the extra line has not been removed.

I suppose I could do this manipulation in an OpenCard handler but I would like to understand why it doesn't work.

I'm using LC 8.1.3 Indy

Many thanks

IanMcK
Posts: 16
Joined: Mon Jul 05, 2010 10:47 am

Re: Problems using formmatedtext in a field on another card

Post by IanMcK » Sun Feb 26, 2017 1:07 pm

Ok.So the solution was simple.
I extracted the code which checked the number of lines of formatted text and made it a specific handler positioned in the card script of the 'playcard' card. This handler, which simply looped through the four fields altering as needed, was called in the PreOpenCard hander.
So it would seem that you have to go to the card in question if the formatted text of a field is to be accessed.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Problems using formmatedtext in a field on another card

Post by FourthWorld » Sun Feb 26, 2017 6:41 pm

IanMcK wrote:So it would seem that you have to go to the card in question if the formatted text of a field is to be accessed.
It's rare that it's necessary to go to a card to access data stored on it, but formattedText is an exception that's understandable once we take a moment to grok how it works:

You can get the text of a field, and even any of the special formats from a field (htmlText, rtfText, even the styledText array) from anywhere. But the formattedText is a special case that requires not only a complete unpacking of the card record, but also a rendering of the text within the field in order to calculate the line wraps. It's one of the few cases where the data you retrieve is dependent on how it's displayed. So many details come into play with that rendering (font metrics, field properties like margins, any visual properties inherited from the card like font, size, etc.) that the way to trigger a complete render is to go to the card where the field resides.

So the way to handle that, as you've found, is to lock the screen, go to the card, get the formattedText, and then return to the card you want to work with that on.

If performance is critical, or if you just want to avoid triggering any handlers related to going to the card (openCard, preOpenCard, etc.), in addition to "lock screen" you can also "lock messages" before going to another card.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

IanMcK
Posts: 16
Joined: Mon Jul 05, 2010 10:47 am

Re: Problems using formmatedtext in a field on another card

Post by IanMcK » Sun Feb 26, 2017 7:14 pm

Thanks for the reply Richard and the explanation. I had wondered if that was the case because, as you said, its logical. Good to have it confirmed.

Regards

Post Reply

Return to “Talking LiveCode”