Group objects "Lessons Learned" & question

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Group objects "Lessons Learned" & question

Post by chipsm » Tue Mar 28, 2017 4:38 pm

I bring items to the forum for 2 reasons: 1. to get answers/advice from more experienced users and 2 . to expose other users to problems that I am having with LiveCode.
Call this "Lessons Learned"
I have been working with Group Objects as of late. I thought I was having good experiences using code that would discover the fields in groups and low and behold I fell into a Rabbit hole and everything went to hell.
this is an example of some of the code:

repeat with xfield = 1 to the number of fields in group "grpTest"
set the lockText of field xField to false
put the short name of field xField into tField
end repeat

This code seemed to work just fine and I was actually able to loop through legitimate fields in my group. But, The idea was to loop through only the fields that are contained in the group and ignore other objects on the card. I only included the fields and not the labels. BUT, as I added other groups onto the card, things got hinky. The code seemed to have lost focus on the named Group and started looking at all of the fields on the card. This was fairly frustrating and required many hours of trying remedy the situation. I tried and re-tried to re-code and exam what was happening. I removed and replaced, renamed groups and everything that I could think of. No such luck. Other weird anomalies were happening also - I seemed to have lost the ability to capture the "On MouseUp" commands and had to use the "right click" of my mouse to make a "MouseUp" command.

Finally I was forced to the LiveCode dictionary to see what was happening.
I discovered the "the ChildControlName" command. I had to use a little more of a verbose script to do the same thing but it worked.
Sample Code:
put the childControlNames of group "Test" into grList
# ENABLE PROJECT INFORMATION GROUP
# ALL FIELDS ARE DEFAULTED TO LOCKTEXT IS TRUE
repeat with xfield = 1 to the number of lines of grList
put line xfield of grList into tField
set the lockText of field tField to false
set the foreGroundColor of field tField to "red"
end repeat

This works just the way I wanted it to do.

So, Question:
Has anyone used the first bit of code :"repeat with xfield = 1 to the number of fields in group "grpTest" or similar with any success? Especially with multiple groups on a card. I was obviously wrong with my assumptions of how this was to work - or is this a bug in LiveCode that needs to be placed in a bug report? I know that Groups with Nested Groups can throw the message path for a loop. But this is not a nested group.
Any comments or recommendations would greatly be appreciated. If not for myself but for the education of other LiveCoders out there.
Clarence Martin
chipsm@themartinz.com

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Group objects "Lessons Learned" & question

Post by Klaus » Tue Mar 28, 2017 4:51 pm

Hi Clarence,

not sure I understand you correctly, but if you use this -> repeat with xfield = 1 to the number of fields OF group "grpTest"
then you need to:

Code: Select all

...
repeat with xfield = 1 to the number of fields OF group "grpTest"
  set the locktext of fld xFiled OF GRP "grpTest"
  ## more stuff...
end repeat
...
Since xField is just a number and LC will start with field 1 on the current card if you omit "...of grp "grpTest"!


Best

Klua

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Group objects "Lessons Learned" & question

Post by chipsm » Tue Mar 28, 2017 5:04 pm

Hi Klaus,
are you saying the key word for this script is "of" Group, not "in" Group that I was using?
Clarence Martin
chipsm@themartinz.com

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Group objects "Lessons Learned" & question

Post by chipsm » Tue Mar 28, 2017 5:07 pm

Klaus,
after some thought, using "of" makes sense to me. I didn't think about that.
Thanks again you are always most helpful with my questions.
Clarence Martin
chipsm@themartinz.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Group objects "Lessons Learned" & question

Post by dunbarx » Tue Mar 28, 2017 5:08 pm

Hi.

If I get your issue, why not simply include a "filter" in the loop (pseudo):

Code: Select all

if the owner of fld xField = theGroupOfInterest then...
This may run into trouble with nested groups, if the groupOfInterest is an enclosing group, but you can script your way out of that as well, by checking to see if the owner of the groupOfInterest is also a group, etc...

Craig Newman

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Group objects "Lessons Learned" & question

Post by chipsm » Tue Mar 28, 2017 5:12 pm

Craig,
That may work, I'll explore that.
It does seem to be a little more code, depending on how many objects are on your card.
Thanks, all suggestions are valuable even for later use.
Clarence Martin
chipsm@themartinz.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Group objects "Lessons Learned" & question

Post by dunbarx » Tue Mar 28, 2017 5:16 pm

Hi.

"of" and "in" are synonyms, I think in all cases, but do not hold me to that. Anyway, for me, "of" is my go-to keyword, since I read:

Code: Select all

the number of lines of soAndSo
answer the loc of fld 1
more naturally than

Code: Select all

the number of lines in soAndSo
answer the loc in fld 1
Otherwise it is a matter of style.

Craig

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Group objects "Lessons Learned" & question

Post by chipsm » Tue Mar 28, 2017 6:13 pm

OK, my report back.
Klaus, using "OF" group in the code above does not work, nothing changed.
I went back to the code and tried.
Craig, I have not tried the "if the owner of fld xField = theGroupOfInterest then..." yet. But I may use that elsewhere.
But apparently Of and The are not the same and neither work in this use case.
Clarence Martin
chipsm@themartinz.com

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Group objects "Lessons Learned" & question

Post by Klaus » Tue Mar 28, 2017 6:14 pm

Hi Clarence,
chipsm wrote:Hi Klaus,
are you saying the key word for this script is "of" Group, not "in" Group that I was using?
no, the key to "success" in this case is the addtion of "...of/in grp "groupTest" at all, as my last sentenced explained.

I was only emphasizing the OF since this is the official syntax, although IN will work, too,
and I have been using LC since the MetaCard days in 1999 and found (and experienced!) that
the engine gets a bit pickier with every new release and so one day IN might be deprecated
and you will be happy to have stuck to the official syntax ever since! :D


Best

Klaus

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Group objects "Lessons Learned" & question

Post by Klaus » Tue Mar 28, 2017 6:17 pm

Hi Clarence,
chipsm wrote: Klaus, using "OF" group in the code above does not work, nothing changed.
hm, don't get it!?
Just made a test with some grouped fields and this script:

Code: Select all

on mouseUp
   repeat with i = 1 to the num of fields of grp 1
      set the locktext of fld i of grp 1 to TRUE
   end repeat
end mouseUp
and it worked as advertized!
What am I missing?


Best

Klaus

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 244
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Group objects "Lessons Learned" & question

Post by chipsm » Tue Mar 28, 2017 7:24 pm

Klaus,
I don't know.
when I initially did this script, it worked. Now,it does not recognise my group and stars with other objects on my card.
I am completely baffled.
I am just going to use ""the ChildControlName" command for now. It has been working flawlessly for now.
I even tried the LiveCode version 8.1.2 instead of the 9.0 (dp 6).
I'll post any problems using this my changes.
Clarence Martin
chipsm@themartinz.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Group objects "Lessons Learned" & question

Post by jacque » Wed Mar 29, 2017 5:59 pm

Klaus is pointing out that you need to include a complete field reference inside the loop, not just in the repeat declaration. It works fine if you remember to do that.

Inside the loop, the engine has no idea that the field number in the variable means the number of the field relative to the group. It will act on the field relative to the card unless you specify the group it belongs to.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Group objects "Lessons Learned" & question

Post by dunbarx » Thu Mar 30, 2017 6:42 pm

What Jacque said.

This is very important, and not well documented.

Make five buttons on a new card. They will be numbered from 1 to 5. Well and good. Now group the last two.

From the msg box,

Code: Select all

select btn 1
The first btn is selected.

Now

Code: Select all

select btn 1 of grp 1
The "fourth" btn is selected.

Craig

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Group objects "Lessons Learned" & question

Post by Klaus » Thu Mar 30, 2017 6:52 pm

I really thought I had put that clear!
Not? 8)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Group objects "Lessons Learned" & question

Post by jacque » Thu Mar 30, 2017 8:04 pm

It was clear to me. :) But after I saw the OP's confusion I explained it in different words.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Talking LiveCode”