Page 1 of 1
How to keep selected text selected?
Posted: Mon Dec 21, 2009 5:46 pm
by hamlynart
Hi Folks,
Here's my problem. When I click the button my selected text becomes deselected. How do I keep it selected?
Code: Select all
on mouseUp
put the selectedText into tSelect
put "</font>" after tSelect
put "<font color ="& quote & "red" & quote &">"before tSelect
put tSelect into the selectedText
end mouseUp
Thanks in advance.
Jim H
Re: How to keep selected text selected?
Posted: Tue Dec 22, 2009 11:57 am
by bn
Hi Jim,
I think it is part of the windows interface that a field looses focus if you click a button, on a mac this is not the case.
I would trapp the loosing of focus in a script of the field and set a custom property of that field that you than query:
Code: Select all
on focusOut -- for locked text fields
set the cLastSelection of me to the selectedChunk
put "out"
end focusOut
on exitfield -- for editable fields
set the cLastSelection of me to the selectedChunk
put "out"
end exitfield
Than you set the script of the button to:
Code: Select all
on mouseUp
set the textcolor of the cLastSelection of field 1 to red
end mouseUp
If you want this for multiple fields I would set a custom property of the card (e.g. the cLastSelection of this card) and then query that in the button script.
The way you approached it before you were mixing the raw html of a field. This would have to be done in the htmlText of field xyz. And than set the htmlText of field xyz to the changed htmlText.
If you only want to change color textsize and style it is easier to use the syntax I propose.
regards
Bernd
Re: How to keep selected text selected?
Posted: Tue Dec 22, 2009 4:17 pm
by hamlynart
Hi Bernd,
Thanks for that. Unfortunately it's not working too well.
When I click the button nothing happens - because the exitfield hasn't happened until I click outside the box (clicking a button doesn't register).
So, if I select a chunk and click my button and then click outside the field I get the "out" message. Then if I click the button again the text that I had selected changes to red but it isn't selected.
?
I'm using a Mac too.
Cheers
Jim H
Re: How to keep selected text selected?
Posted: Tue Dec 22, 2009 5:57 pm
by bn
Jim,
I thought you were on a windows computer. So I actually tested by clicking outside of the field and then clicking the button.
On a mac it is easier (assuming that I am correct that on a windows computer a field looses focus when a button is clicked), almost your original approach:
this works on locked text and on editable text, the selection is not lost.
Code: Select all
on mouseUp
if the selectedText is not "" then
set the textcolor of the selectedText to red
end if
end mouseUp
regards
Bernd
Re: How to keep selected text selected?
Posted: Tue Dec 22, 2009 6:36 pm
by dunbarx
It is possible to lose these text functions based on all kinds of subsequent actions. To save them
Code: Select all
on mouseUp
put the selectedChunk into tChunk
doStuff
do "put" && "yourtText" && "into" && tChunk
end mouseUp
This usually needs to be done in the first line of the handler
Re: How to keep selected text selected?
Posted: Tue Dec 22, 2009 8:31 pm
by hamlynart
Bernd - Nice - that works well with your textcolor code but not with my HTML code. I should have explained - the HTML that was in my code is intended to work thus:
I select word 2 from the following:
One two three.
I click my button
I get:
One <font color ="red">two</font> three.
And word 2 should still be selected.
I could, of course, use HTMLtext but that produces loads of paragraph code (<p> etc) which I don't want or need.
Jim H
Re: How to keep selected text selected?
Posted: Tue Dec 22, 2009 11:12 pm
by bn
Jim,
I dont quite understand. What do you want to display?
One Two(in red) Three
or
One <font color ="red">two</font> three
The word two should be red or do you want the html in the text?
What do you want to be selected after your operation? Is it the word 'two' that should be selected?
Could you explain a little more the context of what you want to achieve?
regards
Bernd
Re: How to keep selected text selected?
Posted: Wed Dec 23, 2009 2:32 pm
by hamlynart
Hi again,
Ahh yes I see where I've been really unclear.
I want the HTML code displayed. and I'd like the word two to be selected (or the HTML and two would be fine too).
The reason I need this is because I'm making a simple HTML code tool which will add simple HTML formatting to text. I have a palette with buttons for Italic, Bold, Bold+Italic and red.
In some instances I'd like to have bold and red and I don't want to have to reselect the same text each time.
Hope this makes some more sense?
Cheers
Jim H
Re: How to keep selected text selected?
Posted: Wed Dec 23, 2009 8:12 pm
by bn
Jim,
sorry I was a bit confused.
try this:
Code: Select all
on mouseUp
-- get the char count of the selection
-- e.g. char 34 to 38 of field 1
put the selectedChunk into tSelectChunk
put the selectedText into tSelect -- get the text
-- a little errorchecking
if tSelect = "" then exit mouseUP
put "</font>" after tSelect -- do your html
put "<font color = "& quote & "red" & quote & ">" into tbefore -- split html
put tbefore before tSelect
put length (tbefore) into tLength
-- add leading html to the selectedChunk to hilite /change color of the original word
add tLength to word 2 of tSelectChunk
add tLength to word 4 of tSelectChunk
put tSelect into the selectedText
-- change color
set textcolor of tSelectChunk to red
-- restore selection
select tSelectChunk
end mouseUp
it should do what you want.
regards
Bernd
Re: How to keep selected text selected?
Posted: Wed Dec 23, 2009 9:17 pm
by hamlynart
Utterly Perfect!
Merry Xmas to you Bernd
Jim H