Unexpected results when deleting a blank line from a list field

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 916
Joined: Wed Nov 04, 2009 11:41 am

Unexpected results when deleting a blank line from a list field

Post by Simon Knight » Sun Jul 30, 2023 9:21 am

My application has a list field and I want the user to be able to edit its contents. I found this post "Click, Pause, Click Again to Edit a Line in a Field?" and used the simple code by dunbarx in my list field.

Code: Select all

on selectionChanged
   --answer the selectedtext of field "ServerList" 
   --answer the hilitedLine of field "ServerList"
   set itemdel to "/"
   put the last item of the folder into tFolderName
   
   put the selectedtext of me into tText
   set itemdel to cr
   put item 1 of tText into tText
   
   put tText into field "AddMe"
   put tText & "/" & tFolderName into field "SelectedServer"
end selectionChanged

on mouseup
   wait 15 milliseconds
   // mouseclick goes true if user makes second click
   if the mouseClick then
      set the locktext of me to "false"
      set the listbehavior of me to "false"
      Beep
   end if
end mouseup

on mouseLeave
   set the locktext of me to "true"
   set the listbehavior of me to "true"
end mouseLeave
It works but is exhibiting an odd feature: when in edit mode the list field reverts to being a normal field but becomes a list field once the mouse leaves the field. When editing its possible to in effect delete whole lines by using the delete key, this is desirable but the user may fail to delete the New Line character meaning that the line appears as a blank row in the list. Once back in list mode these rows may be selected but deleting them has thrown up an oddity. I use the following code, in a button, to delete a single selected row from the list :

Code: Select all

on mouseUp
  
   delete Line  (the hilitedLine of field "ServerList") of field "ServerList"
  
end mouseUp
While this works on lines that contain text it fails on lines that are just the newline character. The reason appears to be that hilitedline fails to return a line number.

I know this because in order to discover that no line number is returned I added a line of code to answer the value :

Code: Select all

on mouseUp
   -- Delete the selected line
   answer the hilitedLine of field "ServerList"
   delete Line  (the hilitedLine of field "ServerList") of field "ServerList"
end mouseUp
This prints a blank line but then the selected line does get deleted from the list.

I also note that if I try to add blank lines above text lines using the inspector in the IDE the IDE pauses then removes them.

So what obvious thing am I missing ?

I have created a stack that shows the issue.

best wishes
Simon
Attachments
DeletingFromList.livecode.zip
Small demo stack to see the issue
(1.8 KiB) Downloaded 93 times
best wishes
Skids

stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

Re: Unexpected results when deleting a blank line from a list field

Post by stam » Sun Jul 30, 2023 11:50 am

Simon Knight wrote:
Sun Jul 30, 2023 9:21 am
While this works on lines that contain text it fails on lines that are just the newline character. The reason appears to be that hilitedline fails to return a line number.
Hi Simon - I may have misunderstood, but both 'delete' buttons delete empty lines with no issues. Adding new empty lines in the middle of the list doesn't change this behaviour. Is that what you are asking?

If it's not working in your app, it may be that you haven't isolated the issue, because this test stack seems to work as advertised, unless I misunderstood?
Regards
Stam

Simon Knight
Posts: 916
Joined: Wed Nov 04, 2009 11:41 am

Re: Unexpected results when deleting a blank line from a list field

Post by Simon Knight » Sun Jul 30, 2023 12:30 pm

Hi Stam,

No I think you have understood: when I select a blank line and press the top delete button, the one without the "answer" nothing happens, however when using the lower button with the answer line a blank message is displayed and the line gets deleted. Here is a screen shot of the test app when the lower delete is pressed.
Screenshot 2023-07-30 at 12.22.57.png
Screen shot showing the blank answer pane.
It seems that in my rush to post I left an unnecessary "put" in the uppermost handler not that it changes the result.

What I am seeing is that adding the line "answer the hilitedLine of field "ServerList" enables the delete to work, without it nothing happens. Odd!

I'm on MacOS Ventura and using Livecode version 10.0.0 dp 4.

thanks for looking,
S
best wishes
Skids

stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

Re: Unexpected results when deleting a blank line from a list field

Post by stam » Sun Jul 30, 2023 12:34 pm

Also, your code that changes the list field to a a normal field didn't really work for me (LC 9.6.9). If I click multiple times in rapid succession I can sometimes get this to work but not reproducibly and of course I need to move the mouse all the way out of the field to reset it.

an alternative way to do this may be:
1. track which line was clicked with a script local variable - the idea being that if clicking on an already selected line then unlock field
2. track the state of the field (list or normal) with a script local variable
3. Determine whether the field should be converted to a normal field in mouseDown
4. If to be unlocked check the locked status in mouseUp
5. use mouseMove to determine if field should be locked again - if the mouse moves to a different line then lock the field again

Here's an example script:

Code: Select all

local sClickLine, sLocked

on mouseDown
    if the clickLine <> sClickLine then
        put the clickLine into sClickLine
        put true into sLocked
    else
        put false into sLocked
        set the listbehavior of me to false
        set the locktext of me to false
    end if
    pass mouseDown
end mouseDown

on mouseMove
    lock screen
    if sClickLine is not empty and the mouseLine <> sClickLine then
        put true into sLocked
        set the listbehavior of me to true
        set the locktext of me to true
        filter lines of me without empty // get rid of empty lines
        select sClickLine
    end if
    pass mouseMove
end mouseMove

on mouseUp
    if sLocked then pass mouseUp
    select after sClickLine
end mouseUp
regards
Stam

** edited to refactor code
Last edited by stam on Sun Jul 30, 2023 12:54 pm, edited 1 time in total.

Simon Knight
Posts: 916
Joined: Wed Nov 04, 2009 11:41 am

Re: Unexpected results when deleting a blank line from a list field

Post by Simon Knight » Sun Jul 30, 2023 12:39 pm

Yes I have to agree - its all a bit flakey : I'm planning on trying to create a custom list with a second field that is used for input and editing. I think I may have one somewhere but I can't find it, although I have just rediscovered Bernd's ModTableField and Scott Rossi's TableLab which have distracted me. :D

Thanks for your code which I will try and report back.

best wishes
Simon
best wishes
Skids

stam
Posts: 2996
Joined: Sun Jun 04, 2006 9:39 pm

Re: Unexpected results when deleting a blank line from a list field

Post by stam » Sun Jul 30, 2023 12:44 pm

I must say I still cannot reproduce your deletion problems... with the stack you provide, if I click on an empty line it deletes it just fine with the top 'delete' button and with the bottom 'delete' button. I get the line number in the answer dialog instead of a blank dialog like you show with your screenshot...

Here's a screen shot after using the top and then bottom delete button - all seems to work as advertised:
delete.jpg

Simon Knight
Posts: 916
Joined: Wed Nov 04, 2009 11:41 am

Re: Unexpected results when deleting a blank line from a list field

Post by Simon Knight » Sun Jul 30, 2023 12:53 pm

Ghosts in my machine then :-(
best wishes
Skids

Post Reply

Return to “Talking LiveCode”