vscroll not being updated thro handler

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

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 220
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

vscroll not being updated thro handler

Post by kaveh1000 » Thu Jan 17, 2019 10:25 am

Hi Folks

My second post on scrolling in 24 hours!

I have a handler that acts on a field. At the start it reads the current vscroll and resets it at end, so

Code: Select all

put the vscroll of fld "text" to tScroll
... [modify text]
set the vscroll of fld "text" to tScroll
But, when the handler finishes, the field is always at vscroll of 0. I have tried "wait 10 ticks", locking or unlocking screen, and also putting the resetting of scroll in another handler, so:

Code: Select all

put the vscroll of fld "text" to tScroll
... [modify text]
send correct_scroll to me in 10 ticks
...

on correct_scroll
set the vscroll of fld "text" to tScroll
end correct_scroll[/code]

But still not working. I note that if I put

Code: Select all

answer tScroll
just before setting the vscroll, then it does work.

Any hints as to what is happening? I do have other things going on in the stack but I can't pin down the problem.

Any advanced ways of debugging, e.g. to see what messages are being seen by the field?

Thank

Kaveh
Kaveh

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

Re: vscroll not being updated thro handler

Post by Klaus » Thu Jan 17, 2019 11:36 am

Hi Kaveh,

did you define tScroll as a local or global variable?
If not, you should. :-)

Best

Klaus

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 220
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: vscroll not being updated thro handler

Post by kaveh1000 » Thu Jan 17, 2019 12:49 pm

Hi Klaus

Actually in my script it is sScroll and it is a local variable.

I just tried global too (only in that script) and not working.

Why would I need it to be local or global? How come putting in "Answer" then allows it to work? I would love any technical insight. :-)
Kaveh

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

Re: vscroll not being updated thro handler

Post by Klaus » Thu Jan 17, 2019 1:10 pm

Actually in my script it is sScroll and it is a local variable.
put the vscroll of fld "text" to tScroll
... [modify text]
send correct_scroll to me in 10 ticks
...
on correct_scroll
set the vscroll of fld "text" to tScroll
end correct_scroll
sScroll, aha... 8)
Why would I need it to be local or global?

I can only comment what I read here, and your example script shows two different handlers using the same variable tScroll, see above. And for that we need tScroll to be local or global!
How come putting in "Answer" then allows it to work?
I have no idea.

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 220
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: vscroll not being updated thro handler

Post by kaveh1000 » Thu Jan 17, 2019 1:53 pm

Ah, understood. Yes, I should have been clear it was local.

Thanks Klaus...
Kaveh

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

Re: vscroll not being updated thro handler

Post by bogs » Thu Jan 17, 2019 2:35 pm

heya kaveh,
Interesting problem, but seems like you have a communication breakdown. I am curious about something though, it seems like you are trying to re-invent something the field does all on its own by default, i.e. when you type in a field, the scroll automatically drops down when the text is changed. Not a problem, but why?

First, what Klaus is saying is absolutely right, if you want a variable to work across 2 separate handlers, the easiest way would be to put it outside of the handlers and into the script at an appropriate location.

The second thing I would suggest is putting your relevant (to the problem) code into one code block instead of the three you have in that first post, to make it easier to follow (see below).

The last thing I would suggest is that since you list a handler to correct the scroll, you not send things back to themselves but instead just call the handler. As a side note, I would also not name your fields with reserved words, like 'text'! Also, tScroll sScroll :wink:

Here is your code as copied from the first post, but with 'tScroll' listed as script local variable [ I put in psuedo handlers since they weren't included for illustration only]-

Code: Select all

local tScroll

on textChanged
	put the vscroll of fld "text" to tScroll // field named "text" ?!? Please change this!
	... [modify text]
	set the vscroll of fld "text" to tScroll
end textChanged

on enterInField
	put the vscroll of fld "text" to tScroll
	... [modify text]
	send correct_scroll to me in 10 ticks
	...
end enterInField

on correct_scroll
	set the vscroll of fld "text" to tScroll
end correct_scroll
You have a handler, 'correct_scroll', but you don't call it like it would make sense to use it, unless you really need that 10 tick delay. If you really want to avoid using a script level variable, I would just call the handler each time (of course, you'll have to beef that handler up a bit more).
An Example of this would be like -

Code: Select all

// using a handler only to correct the scroll...
on mouseDown
   // set the scrollbar to the top of the field...
   set the vscroll of fld "myField" to 1
   
end mouseDown

on mouseUp
   // call the handler...
   correct_scroll
end mouseUp

on correct_scroll
   // set the vscroll to the bottom of the field...
   set the vscroll of fld "myField" to the formattedHeight of field "myField"
end correct_scroll
The above code produces this effect -
Selection_001.png
Mouse down...
Selection_001.png (7.21 KiB) Viewed 410 times
Selection_002.png
Mouse up!
Selection_002.png (6.11 KiB) Viewed 410 times
Now, locating the variable in a handler means that as each handler finishes, the variable becomes empty. You *could* do something like -

Code: Select all

on mouseUp	
	local tScroll // local inside of handler...
	put the vscroll of fld "text" to tScroll
end mouseUp
which will allow you to use it where ever else you include the line 'local tScroll' within the script level, but that is far less efficient than doing it the way I modified your code up there in the first block, and even that is far less efficient than just making the handler 'correct_scroll' do the work.

I'd really encourage you to stay away from global variables if you can possibly avoid them at this stage, they can make life way too interesting in ways you don't need it to be.

Hope that helps :D

*Edited to make better sense and correct error sentences :P
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1986
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: vscroll not being updated thro handler

Post by [-hh] » Thu Jan 17, 2019 3:16 pm

That it works with an answer in between shows: The problem is in your [modify text].
Just try the following and report back:

Code: Select all

lock messages ##<--
put the vscroll of fld "text" into tScroll
... [modify text]
set the vscroll of fld "text" to tScroll
And change in all posts above
put the vscroll of fld "text" to tScroll
to
put the vscroll of fld "text" into tScroll
shiftLock happens

Post Reply

Return to “Talking LiveCode”