Hiliting Words in a Field

Jan Schenkel janschenkel at yahoo.com
Mon Jun 7 16:10:56 CDT 2010


--- On Mon, 6/7/10, Gregory Lypny <gregory.lypny at videotron.ca> wrote:
> Hello everyone,
> 
> I have a data field with many lines and I'd like to script
> a handler that hilites every appearance of a particular word
> in yellow?
> 
> Regards,
> 
> Gregory
> 

It depends a bit on your definition of the term 'word' in this context.

The straightforward solution is to do a replace in the htmlText of the field, wrapping it with a font tag - but this simply hilites every occurence of the search string, rather than the whole word:
##
put the htmlText of field "Data" into tHtmlText
replace "foo" \
    with "<font bgcolor=" & quote & "yellow & quote & ">foo</font>" \
    in tHtmlText
set the htmlText of field "Data" to tHtmlText
##

If you're looking to hilite only complete words, your best option is the wordOffset function and a loop:
##
put the text of field 1 into tText
set the wholeMatches to true
put wordOffset(tWordToFind, tText) into tWordOffset
put 0 into tPrevOffset
repeat until tWordOffset = 0
  add tWordOffset to tPrevOffset
  set the backgroundColor of word tPrevOffset of field 1 to "yellow"
  put wordOffset(tWordToFind, tText, tPrevOffset) into tWordOffset
end repeat
##
If you leave the 'wholeMatches' local property at its default value of 'false' the above script hilites the entire word, if part of it matches tWordToFind.

HTH,

Jan Schenkel
=====
Quartam Reports & PDF Library for Revolution
<http://www.quartam.com>

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)


      




More information about the use-livecode mailing list