Field number in a group

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Field number in a group

Post by paulclaude » Wed May 22, 2019 10:11 am

A probably simple question:

I can identify a field in a group by its order inside the group:

Code: Select all

put "this" into fld 3 of group "test" of group "main"
but if I know only the fld ID, how can I get its order number (3 in my example) in that group (directly, without a repeat loop scripting)?

This

Code: Select all

put the number of fld ID myID of group "test" of group "main"
only returns the field number relative to all fields of main group.

Thanks

Paul

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

Re: Field number in a group

Post by bogs » Wed May 22, 2019 10:51 am

While I don't know the answer to your actual question, I'm curious to know why if you have the fields id, you don't just set its number to whatever you like, which would not require a loop.

Does the number have to stay the number for some other code in the program?
Image

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Field number in a group

Post by paulclaude » Wed May 22, 2019 12:59 pm

I need to obtain the field number in the group because I must know the "next" field, so adding 1 to that number brings me to it.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Field number in a group

Post by jmburnod » Wed May 22, 2019 1:26 pm

Hi,
What bogs said.
Just to be sure you want the number or the layer of a field
If you want the number of a fld I think you can't avoid a loop
Best regards
Jean-Marc
https://alternatic.ch

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Field number in a group

Post by jmburnod » Wed May 22, 2019 1:46 pm

Why you dont use the name of your flds "MyField_1", "MyField_2" for group flds ? You may get the name of your fld at openfield, put it into a variable or a customprop of your group, add 1 to this name to know the name of next fld.
https://alternatic.ch

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

Re: Field number in a group

Post by dunbarx » Wed May 22, 2019 7:04 pm

Hi.

ALWAYS name controls that have even a remote chance of being called upon in any referential manner. NEVER try to use layer number.

I use, for fields, "f1", "f2" at a minimum, and more descriptive names, like "city1", "city2" as needed. It is nice to be able to "read" which control is which during any sort of processing.

Are you comfortable with being able to deal with that sort of thing in, say, a repeat loop? Assuming you have some fields each named "f1", "f2", etc.

Code: Select all

on mouseUp
   repeat with y = 1 to the number of flds
      put y * 10 into fld ("f" & y)
   end repeat
end mouseUp
Now you never have to think about it ever again. The naming convention does all the work.

Craig

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

Re: Field number in a group

Post by bogs » Wed May 22, 2019 9:35 pm

dunbarx wrote:
Wed May 22, 2019 7:04 pm
Are you comfortable with being able to deal with that sort of thing in, say, a repeat loop?
Oh now come on here! If we could have stuck up a repeat loop, this could have been answered probably in my post!
paulclaude wrote:
Wed May 22, 2019 10:11 am
how can I get its order number (3 in my example) in that group (directly, without a repeat loop scripting)?
...and just for the record, I completely agree with Jean-Marc and Craig, name the fields sensibly and use a loop, it really is the easiest fastest way to get there.

IF for instance, the fields were named in almost any manner that is reasonable, you could compare the names against the childControlNames of group x, and then put the layer of that control into a variable or whatever. Not that you can't do it without reasonable names either.
Selection_001.png
How unreasonable can you get ?
From what I saw setting that picture up, the groups get layered first, then the controls for each group, i.e. First I put 2 buttons on the card, then grouped them, then put 2 fields, then grouped them all into a 2nd group, but the numbering goes outside group, inside group, button, button, field and finally field, just as it looks in the project browser.

*Edit - substitute the word "number" for "layer" and it is what you were asking about.
number

Type: function

Syntax:
the number of [card|cd|background|bkgnd|bg] {objectType | parts | controls}
If it is a locked field, you can also look for the target or last target.
Image

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Field number in a group

Post by paulclaude » Thu May 23, 2019 12:13 pm

Thank you all, I know there are many ways to refer to a field simulating its number (name, property) or to retrieve its actual number within a group (repeat loop).

My only doubt was whether a Livecode direct function existed (with the right parameters) to know this number: it does not exist, I take note of it.

Greetings :wink:

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

Re: Field number in a group

Post by bogs » Thu May 23, 2019 1:02 pm

paulclaude wrote:
Thu May 23, 2019 12:13 pm
My only doubt was whether a Livecode direct function existed (with the right parameters) to know this number: it does not exist, I take note of it.
If that is what you got out of what I posted last, I completely failed in my explanation.
Image

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Field number in a group

Post by paulclaude » Thu May 23, 2019 3:27 pm

bogs wrote:
Thu May 23, 2019 1:02 pm
paulclaude wrote:
Thu May 23, 2019 12:13 pm
My only doubt was whether a Livecode direct function existed (with the right parameters) to know this number: it does not exist, I take note of it.
If that is what you got out of what I posted last, I completely failed in my explanation.
Hi Bogs,maybe my explanation is misspelled (sorry for my elementary English).

I know many workarounds to resolve this problem. I only asked for a direct Livecode function (if it exists).

Example: I have a group "B" within another group "A". This group "B" only have 3 fields. Knowing only the target field ID (field that is the second of this group "B"), if I write:

Code: Select all

put  the number of fld ID 6890 of group "B" of group "A"... 
it gives me 13.
If I write:

Code: Select all

put  the layer of fld ID 6890 of group "B" of group "A"... 
it gives me 64.
I needed the function returns 2

That was my original problem.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Field number in a group

Post by jmburnod » Thu May 23, 2019 4:30 pm

Hi PaulClaude,
I think there is not a direct Livecode function to do what you want, but you may roll your own function
Best
Jean-Marc
https://alternatic.ch

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Field number in a group

Post by paulclaude » Thu May 23, 2019 4:47 pm

jmburnod wrote:
Thu May 23, 2019 4:30 pm
Hi PaulClaude,
I think there is not a direct Livecode function to do what you want, but you may roll your own function
Best
Jean-Marc
Yeah, I think so too.

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

Re: Field number in a group

Post by bogs » Thu May 23, 2019 4:58 pm

Then you need to learn to use and love childControlNames, because you are not looking for the number or layer of the field, but it's order within the group.

This video may make it clearer for you, but in essence, to find the order of a control within a group within a group within yet ANOTHER group is no more difficult than figuring out its line number, which you can do with code from the previous post, and without a repeat loop.
Image

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

Re: Field number in a group

Post by dunbarx » Thu May 23, 2019 5:04 pm

Hi.

This?

If I have a group ("grp2") of fields that are within another group ("grp1") of fields, and I want the ID of the field in grp "grp2" that has the ID "targetID":

Code: Select all

on mouseup
   repeat with y = 1 to the number of flds in grp "grp2"
      if the id of fld y of grp "grp2" = targetID then answer y
   end repeat
end mouseup
I get the number of the field I want. LC does the heavy lifting, since it only looks in the group you specify. More directly, you can simply get the second field in that group.

Am I missing it?

Craig

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Field number in a group

Post by paulclaude » Thu May 23, 2019 5:32 pm

dunbarx wrote:
Thu May 23, 2019 5:04 pm
Hi.

This?

If I have a group ("grp2") of fields that are within another group ("grp1") of fields, and I want the ID of the field in grp "grp2" that has the ID "targetID":

Code: Select all

on mouseup
   repeat with y = 1 to the number of flds in grp "grp2"
      if the id of fld y of grp "grp2" = targetID then answer y
   end repeat
end mouseup
I get the number of the field I want. LC does the heavy lifting, since it only looks in the group you specify. More directly, you can simply get the second field in that group.

Am I missing it?

Craig
Yes, this is the loop "workaround" (that I've previously mentioned) that I actually use, Craig.

Post Reply