How to delete the selected text

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

How to delete the selected text

Post by alemrantareq » Mon Dec 28, 2009 8:24 am

Hi everybody, I need a help. I have some contents in a scrolling field and I want to delete the selected from that list. I've tried a lot through delete, clear, put empty commands and also selectedtext, selection, selectedline and selectedchunk functions to do that but failed.

Code: Select all

on mouseUp
  put the selectedtext of fld "asd" into myvar
  delete myvar
end mouseUp
and also -

Code: Select all

on mouseUp
  put the selectedtext of fld "asd" into myvar
  put empty into myvar
end mouseUp
But all have failed. How to do that? Please help...

Peter Wood
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Mon Jul 06, 2009 4:53 am
Location: Bamboo River

Re: How to delete the selected text

Post by Peter Wood » Mon Dec 28, 2009 9:56 am

I tried this with a scrolling field called Field, putting the script in a Button, it worked for me:

Code: Select all

on mouseUp
  delete the selectedText of field "Field"
end mouseUp

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How to delete the selected text

Post by sturgis » Mon Dec 28, 2009 12:45 pm

try

put empty into the selectedText of field "Field"

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: How to delete the selected text

Post by alemrantareq » Mon Dec 28, 2009 2:07 pm

Before my posting, I've tried all of these codes. And after your replies, I've tried both of them again renaming my field "asd" to "Field". But failed again. I tried this -

on mouseUp
delete the selectedText of field "Field"
end mouseUp

And got this error -
button "Button": execution error at line 2 (Handler: can't find handler) near "the", char 4

Then I tried this one -

on mouseUp
put empty into the selectedText of field "Field"
end mouseUp

And got this error -
button "Button": execution error at line 2 (Chunk: no target found), char 14

I'm using rev ent. 4. As far as I know, the aboves should work; but I'm afraid why they don't?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3975
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: How to delete the selected text

Post by bn » Mon Dec 28, 2009 3:04 pm

Alemantareq,

if you are on windows I think the field looses focus if you click a button. This is not the case on the mac.
To catch this put this into the field

Code: Select all

on focusOut -- for locked text fields
   set the cLastSelection of me to the selectedChunk
end focusOut

on exitfield -- for editable fields
     set the cLastSelection of me to the selectedChunk
end exitfield
this way you make a custom property of the field that gets filled with the last selection. This custom property you can access from your button.
Something like

Code: Select all

on mouseUp
   put the cLastSelection of field 1 into mySelection
   if word 4 of mySelection < word 2 of mySelection then exit mouseUp -- no text selected, just the insertion mark is in the field
   put the number of lines of char 1 to (word 2 of mySelection) of field 1 into tNumOfLines -- find out how many lines up to the begin of the selection
   delete line tNumOfLines of field 1
end mouseUp
If this is the problem then I can not really test it because I dont have access to a windows computer. You might want to experiment with this a bit.
Regards
Bernd

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: How to delete the selected text

Post by alemrantareq » Mon Dec 28, 2009 4:35 pm

Yes, I'm using windows xp and its the text locked field. Thanks Bernd for your reply but it doesn't work for me. Oh ! its really making me disappointed.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How to delete the selected text

Post by sturgis » Mon Dec 28, 2009 5:35 pm

Here just do it like this

Code: Select all

put empty into the selectedText
and it should work. I always forget that selectedText is its own container.

edit: at least this works on my win2k, don't have other test beds available.

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: How to delete the selected text

Post by alemrantareq » Mon Dec 28, 2009 5:53 pm

Thanks a lot sturgis; your correction really works :lol:

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: How to delete the selected text

Post by alemrantareq » Tue Dec 29, 2009 12:29 pm

Putting empty into the selectedText deletes the text but that line turns to an empty line and still exists. Is there any way to delete that empty line?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How to delete the selected text

Post by sturgis » Tue Dec 29, 2009 6:38 pm

My apologies, I thought you were trying to delete part of a line that had been selected IE removing a portion of the text. If you just want to remove all hilited lines in a list field you can do this.

Code: Select all

on mouseUp
   repeat for each item tLine in the hilitedLines of field "Field"
      delete line tLine of field "Field"
   end repeat
end mouseUp

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: How to delete the selected text

Post by alemrantareq » Wed Dec 30, 2009 4:36 am

Using repeat loop was in my mind but I didn't try it thinking that it'd not work as the selectedLine, selectedText of fld "Field" had failed.

However, Thanks a lot (u r great 8) )

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to delete the selected text

Post by SparkOut » Tue Jan 05, 2010 1:16 am

A slight variation may be required if you are deleting multiple lines, as when you delete the first line in the list, then every other line below it will be shuffled up one line, and the next delete will be offset by a line, and that can compound over the repeat loop too. If you sort the hilitedLines list first, so that you delete the highest line number then the next highest, etc, each line number in the list will still match the original selection:

Code: Select all

on mouseUp
   put the hilitedLines of field "Field" into tLines
   sort tLines descending
   repeat for each item tLine in tLines
      delete line tLine of field "Field"
   end repeat
end mouseUp
HTH
SparkOut

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: How to delete the selected text

Post by Simon Knight » Tue Jul 24, 2018 8:33 am

When applying this excellent tip in LC 9 I had to specify that the items were the subject of the sort e.g.

Code: Select all

put the hilitedLines of field "FullList" into tLines
   sort items of tLines descending
   repeat for each item tLine in tLines
      delete line tLine of field "FullList"
   end repeat
best wishes
Skids

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: How to delete the selected text

Post by jmburnod » Tue Jul 24, 2018 8:59 am

Hi,
Is there any way to delete that empty line?

Code: Select all

filter fld "myField" without empty
works (LC 9, OS X 10.13)
Best regards
Jean-Marc
https://alternatic.ch

Post Reply

Return to “Talking LiveCode”