Deleting the contents of a group using code

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
benpaker
Posts: 1
Joined: Sat Jul 18, 2020 3:08 am

Deleting the contents of a group using code

Post by benpaker » Sat Jul 18, 2020 3:10 am

I think this is a beginner level question that I'm really struggling with.

All I want to do is empty a group in code so I can repopulate it.

I have a 'complex (to me)' script that takes a template group and works through a loop to add instances of it into the master group. The problem is when I want to add an item I would just like to clear the master group and then re-run the build to repopulate it with the latest data.

Deleting all items in a group sounds simple but for the life of me I cannot work out the code to do it, all I want is the group I have called

group "grpScroller"

stays on the card, and it is empty read to take new data.

All the data I add to the group also group if that makes sense, I copy a template group into grpscroller, all that is working great, I just can't work out how to wipe the group clean ready for a new set of data.

Should be easy but I'm stuck.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Deleting the contents of a group using code

Post by dunbarx » Sat Jul 18, 2020 4:03 am

Hi. Welcome to the forum.

Groups have an existence apart from any controls that might be members of that group. Try this on a new card. Make a button and then group it. Now, in msg, run "answer the number of btns && the number of groups".

You get "1 1"

Now, again from msg, "delete btn 1", and again "answer the number of btns && the number of groups".

You get "0 1".

The group still lives, even though it has been denuded of all its child controls. So, per your question, if, say, grp "g1" was the group in question:

Code: Select all

on mouseUp
   repeat with y = the number of controls down to 1
      if the name of group "g1" is the owner of control y then delete control y
   end repeat
end mouseUp
Group "g1" is still a resident of the card, albeit "empty", and you can now put new controls into it.

Oh, and do we see why we counted down in the loop instead of the usual up?

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7392
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Deleting the contents of a group using code

Post by jacque » Sat Jul 18, 2020 5:54 pm

Craig's script will do it. But sometimes it's easier to just delete the whole group and create a new one in situations like this. You can put the properties of the group into a variable, delete the group, create a new one, and set its properties to the values saved in the variable. It will be an identical clone of the original but without any content.

Edit: using the templateGroup here will save a line or two of code. Set the properties of the templateGroup to the properties of the original and then just create a new group. It will be identical to the original.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Deleting the contents of a group using code

Post by FourthWorld » Sat Jul 18, 2020 6:23 pm

You can also do a shorter loop that works only on the controls within the group, using the childControldIDs of the group:
https://livecode.com/resources/api/#liv ... controlids

Code: Select all

get the childControlIDs of grp "SomeGroup"
repeat for each line tID in it
   delete control ID tID
end repeat
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Deleting the contents of a group using code

Post by bogs » Sat Jul 18, 2020 9:47 pm

You can also do a loop that works on controls within the group using childControlNames, if you wanted, for instance, to only delete buttons and your not naming the controls in the group, childControlNames gives you a list of the short name of the controls listed.

A buttons name by default is
button "Button"
A field would be
field "Field"

The short name of the button would be Button, the field would be Field. Graphic images are named after their shape, Rectangle, Circle, RoundedRect, and Polygon.

Mind you, this is not as precise as Richard's method, but might be good for a spread shot deletion using a short loop.
Image

Post Reply