Get Value from Scrolling List Field

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

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

DavidBurleigh
Posts: 19
Joined: Sun Apr 05, 2009 9:27 am

Get Value from Scrolling List Field

Post by DavidBurleigh » Sun Sep 13, 2009 8:29 am

List field contains 1,2,3,4,5,infinity; one to each line. The
SLF only shows one item at a time.

what is the code to grab the value that is showing?

The items are not 'selected' in the normal sense, they are
up or down arrowed. apparently I can't just use
put field "spider.depth" into someval

all that does is put the entire list into another field. I need
to be able to put only the 'selected' value into a field, for
a repeat statement.

thanks.
incomex@hotmail.com

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

Post by SparkOut » Sun Sep 13, 2009 8:59 am

Are the "list field" values just sequential numbers? It would be easier just to add or subtract 1 to/from the value in the field on clicking the spinner arrow if so.

Otherwise you could depend on having a fixed line height for the list field and calculating the line number from that, such as

Code: Select all

   put 14 into theFixedLineHeight
   put the vScroll of field "ListField" / theFixedLineHeight into theRawLineNr
   put round (theRawLineNr + 0.55) into theRoundedLineNr
   put line theRoundedLineNr of field "ListField" into field "Destination"
I've broken it down a bit so you can see the steps. When the list is scrolled right to the top, the vScroll is zero, so everything will work quite well going up, but there is a little extra margin at the top and bottom of the field when scrolled all the way, so the vScroll does not always make an exact multiple of the lineHeight. Therefore I've put in a round function to get the nearest integer, and by adding 0.55 it will ensure that the nearest integer is 1 more than the "base" value of zero (ie, if the field is scrolled to the top, then the vScroll is zero, so it would try and return line zero otherwise, when the top line is really line number 1).

HTH,
SparkOut

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

Post by bn » Sun Sep 13, 2009 9:11 am

Hi David,

if I get you correctly than you want do arrow up and down a list field and get the value of the new selection.
The way I would do it is to set the script of the list field to:

Code: Select all

on arrowkey whichKey
   put word 2 of the selectedLine of me into tLineNo
   if whichKey is up or whichKey is left then
      put tLineNo - 1 into tLineNo
      select line tLineNo of me
      put the selectedText of me into field 2
   end if
   if whichKey is down or whichKey is right then
      put tLineNo + 1 into tLineNo
      select line tLineNo of me
      put the selectedText of me into field 2
   end if
end arrowkey
if you dont want to have the value immediately after the selection is made then you could just ask for the "selectedText" of that field from a button or so.
Hope I got you right
regards
Bernd

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5142
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Sep 13, 2009 10:52 am

Hi,

I have created a very simple plugin to create a field with a little arrows button, which should do what you want. It doesn't respond to the arrowKeys on the keyboard though. Maybe I'll to that in another version. Let me know what you think about this plugin. I created it, because the IDE doesn't provide the correct tools to create a well-working little-arrows object.

You can find it here at the bottom of the page ("Small Arrows").

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by bn » Sun Sep 13, 2009 6:34 pm

a version a little different: it rolls the field to the next selection

Code: Select all

on arrowkey whichKey
   -- get current line number
   put word 2 of the selectedline of me into tLineNo
   put the number of lines of me into tLineMax
   
   -- effective works even if no textheight is chosen
   -- then it defaults to the stack defaults usually 14
   put the effective textheight of me into tTextHite
   put the vscroll of me into tvScroll
   
   if whichKey is up or whichKey is left then
      -- exit when at line 1 since there is no line 0
      if tLineNo = 1 then
         beep
         exit arrowkey
      end if
      -- scroll the lines
      repeat with i = 1 to tTextHite 
         set the vscroll of field 1 to tvScroll - i
      end repeat
      
      put tLineNo - 1 into tLineNo
      select line tLineNo of me
      
      -- do what you want with the new value
      put the selectedtext of me into field 2
   end if
   
   if whichKey is down or whichKey is right then
      -- exit when already at max line 
      if tLineNo = tLineMax then
         beep
         exit arrowkey
      end if
      -- scroll the lines
      repeat with i = 1 to tTextHite
         set the vscroll of field 1 to tvScroll + i
      end repeat
      put tLineNo + 1 into tLineNo
      select line tLineNo of me
      
      -- do what you want with the new value
      put the selectedtext of me into field 2
   end if
end arrowkey
put this into the script of the scrolling list field

@ Sparkout
the formula seems to be

Code: Select all

 put the effective textheight of field 1 into tTextHite
   put the vscroll of field 1 into tvScroll
   put ((tvScroll + tTextHite) / tTextHite) into theRawLineNr 
once you have the effective textheight you can set the scroll, or get to the line number
regards
Bernd

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

Post by SparkOut » Sun Sep 13, 2009 8:01 pm

Hi Bernd
I was under the impression the list field is sized to show only one line at a time, ie at standard sizes (here on XP anyway) the field size would be 21 high with a lineHeight of 14.
If I scroll to the top, the increment starts from zero when I start scrolling downwards. Fair enough. If I get to the end of the field, then because of the default padding margin, there is an extra pixel or two - in the case of 10 lines, scrolling right to the bottom returns a value of 10.071429. Heading back up the list, the value will always be out by a little margin. Since you can't then [get line 10.071429 of field "FieldList"] I thought it important to mention, and offer a little rounding feature to get the integer value of the line.

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

Post by bn » Sun Sep 13, 2009 8:17 pm

Hi Sparkout,

I looked into this because I always shunn away from this pixel/scroll thing. I am not shure what about the margins, I think I saw a formula that includes the margins, I think Mark Schonewille posted it sometime.
I would be interested in hints how to calculate the vscroll correctly without a guess factor.

Anyways with a fieldheight set to 21 and an effective textheight of 14 above code works, also if you change the font size.

Anybody knows the secret formula?

regards
Bernd

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

Post by SparkOut » Sun Sep 13, 2009 8:28 pm

Your script is very nice Bernd. It's great that the RunRev community works like this, especially in that there are lots of offerings to help the original user with the question.
I still think it might be much simpler to do it another way, with just adding/subtracting 1 each time the field is scrolled.
Even if the data is not just sequential numbers then you could add/subtract to a count (maybe a custom property of the list field) and "put line theCount of theList into field "theSingleLineDisplayField" or some such.

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

Post by bn » Sun Sep 13, 2009 8:41 pm

I still think it might be much simpler to do it another way, with just adding/subtracting 1 each time the field is scrolled.
that was what I was trying in my first script.
Then I got carried away with the idea of giving the user a feedback. Since if you just have numbers in the scrolling list field and its size is just one line heigh, I had a hard time to see that it actually changed. First I changed the hiliteColor on the fly and then I had the idea of a scrolling list. That of course makes it more complicated.
Your script is very nice Bernd
Thank you. I like the way you approach programming (remember the right button = 3 thing?) and discussing different solutions teaches me a lot.
regards
Bernd

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5142
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Sep 13, 2009 8:52 pm

Hi,

The following might help you.
The maximum scroll equals:


Code:

Code: Select all

the formattedHeight of fld x - the height of fld x - the margins of fld x - the textHeight of fld x

provided that the margins are an integer and the fixedLineHeight of the field is true. If the margins are no integer, you probably need to use item 4 of the margins. If the fixedLineHeight is not true, you need to find a way to figure out the height of the last line of your field. Also, the dontWrap of your field has to be set to true.

This is a nice way to do it:


Code:

Code: Select all

getProp maxScroll
  if word 1 of the name of the target is "field" then
  return (the formattedHeight of the target - the height of the target - the margins of the target - the textHeight of the target)
  else return empty
end maxScroll

(untested, but should work)

Now, if you have this script at stack level or in a library, you can call this property using:


Code:

Code: Select all

put the maxScroll of fld x
set the vScroll of fld x to the maxScroll of fld x

The first line should return an integer, the second should set the scroll of a field to the largest possible value.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by SparkOut » Sun Sep 13, 2009 8:56 pm

Thanks Mark!
and
bn wrote:(remember the right button = 3 thing?) and discussing different solutions teaches me a lot.
regards
Bernd
yes, I remember :D This is one of the things I love about RunRev and the community, and why I like to try and answer where I can, because I always find it helps me to learn and consolidate my knowledge when I do, and these sorts of exchanges create lots of ideas and thoughts for techniques.

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

Post by bn » Sun Sep 13, 2009 9:51 pm

Mark,
thank you for looking into this.
the formattedHeight of fld x - the height of fld x - the margins of fld x - the textHeight of fld x
I get a number in my experimentations that is too low.
If I say

Code: Select all

the formattedHeight of fld x - the height of fld x - the margins of fld x - 1
then I get the right? answer.
I left out the textHeight (isn't that in the formattedHeight?) and subtracted 1 because vscroll starts from 0.
I don't know if my assumptions are right. By setting the vscroll to a ridiculous high number and then querying vscroll I get the maximum of vscroll. That would be the same value I get with the modified formula.
Anybody an idea?

regards
Bernd

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5142
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Sep 13, 2009 10:10 pm

Hi bernd,

I'm quite sure that the formula is correct, unless something has changed in Rev 2.9 or later, which makes this formula obsolete. A few years ago, I used it a lot and I don't think that anything has changed that affects this formula.

You may need to use item 2 of the margins instead of the margines, if you have specified 4 different margins (or maybe I am wrong here and it is item 4 of the margins). Your "-1" is definitely wrong. Have you set the fixedLineHeight of the field to true?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by bn » Sun Sep 13, 2009 11:14 pm

Mark,

I tried it on a different field, fixedLineHeight true, margins 8, dontWrap true and come to the same result. Tried it under version 2.8.1 of RR, same thing.
I don't know.
regards
Bernd

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5142
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Sep 14, 2009 12:13 am

Hi Bernd,

I have checked it and I have seen that it doesn't work completely correctly. I'm pretty sure that the theory is correct, although you may need to correct for borders and the presence of a horizontal scrollbar. I have also noticed that the textSize may affect the value returned by the maxScroll handler, despite fixed textHeight. I'll have too look into this more closely, if I am to find a more general solution. I'll give it some more thought...

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Talking LiveCode”