Page 1 of 1
disable button until condition
Posted: Thu Sep 23, 2010 6:13 pm
by deebee
I would like a button on my stack to remain disabled until a variable is not empty. I'm not exactly sure how to do this. I've tried to do something like:
Code: Select all
global gVariable
on preOpenStack
put empty into gVariable
set the disabled of button "button" to true
end preOpenStack
function waitButton
wait until gVariable is not empty with messages
set the disabled of button "button" to false
end function
Does this just declare the function or does it execute it too? This is not the actual code I'm using, but the structure is identical.
Please help the n00b see the light.
Re: disable button until condition
Posted: Thu Sep 23, 2010 9:03 pm
by Klaus
Hi Dennis,
since the global variable will not be filled surprisingly by strangers out of a sudden

,
couldn't you just add this (set the disabled...) to the handler that fills that global variable?
At least I would do so...
The function you wrote will not work.
Best
Klaus
Re: disable button until condition
Posted: Thu Sep 23, 2010 9:09 pm
by deebee
Thanks for the response!
Actually to be more specific, I need to wait until four different variables contain something before setting the disabled of the button.
Could you steer me in the right direction?
Re: disable button until condition
Posted: Thu Sep 23, 2010 9:12 pm
by Klaus
Hi Dennis,
I am a bit in a hurry right now and this would require a lenghty and not trivial (for beginners) answer.
Maybe I can post something tomorrow (since its 10:15 PM over here in germany).
Best
Klaus
Re: disable button until condition
Posted: Thu Sep 23, 2010 9:16 pm
by deebee
No problems, thanks!
Re: disable button until condition
Posted: Fri Sep 24, 2010 5:59 am
by dunbarx
Hi. I hate globals. Why not set a custom property of the button? And it seems like you were on a good track, thinking that you can send a message to the engine checking on the status of a global variable. But you can also check on the status of the property.
So I made two buttons, one named "xxxx" and another one with the following script in it
Code: Select all
on mouseUp
set the disabled of btn "xxxx" to "true"
set the flag of btn "xxxx" to ""
checkFlag
end mouseUp
on checkFlag
if the flag of btn "xxxx" <> "" then
set the disabled of btn "xxxx" to "false"
exit checkFlag
end if
send "checkFlag" to me in 2
end checkFlag
So the button gets initialized just like you wrote. And then a message is sent constantly checking on the status of the custom property. Your other processes will change the custom property whenever you find yourself in whatever condition that you intended to enable the button. I assume this is the same as when you would have changed your global variable. The cool thing here is that the checking routine goes on in the background, so to speak, while the rest of your application operates happily away.
I hope.
Craig Newman
Re: disable button until condition
Posted: Fri Sep 24, 2010 7:17 am
by shadowslash
Shame on dunbarx for hating on globals! D:<
Anyway, I tried a different approach, I used the
wait[
?] command. I even made you a demo stack which I attached to this post. Please download it and examine what I did, I also made it so that it uses 4 variables based on your previous statement. Please tell me if that was what you were looking for.
P.S. I've been itching to use arrays in my demo but I don't know if using array variables will confuse you more so I continued on with 4 global variables instead namely gVar1, gVar2, gVar3 and gVar4.
Here's a screenshot of my demo stack for you.
FUNFACT:
You are
not a n00b. You are a
newb (newbie). A
noob is a
newbie who causes annoyance to other people commonly by acting intelligent and / or mocking people even though what he / she says is complete crap.

Re: disable button until condition
Posted: Fri Sep 24, 2010 9:15 am
by deebee
Thank you shadowslash! That is simple to understand and it does what I need it to!
dunbarx, thanks for the example! Your solution is a bit trickier for a new kid. I will have to come back to it later to wrap my brain around it!
Re: disable button until condition
Posted: Fri Sep 24, 2010 9:27 am
by shadowslash
deebee wrote:Thank you shadowslash! That is simple to understand and it does what I need it to!
dunbarx, thanks for the example! Your solution is a bit trickier for a new kid. I will have to come back to it later to wrap my brain around it!
No problem! I was also a tad-bit confused when I started with rev, err LiveCode.
Side Note:
To be honest, I actually just thought of that script as I was reading your post and thinking of an easy way for you to understand it. Before, I was actually using the longer, more elaborate, but practically the same-in-terms-of-result, scripts on my projects. Thank you for your thread because I just gained another epiphany with using LiveCode.

Re: disable button until condition
Posted: Fri Sep 24, 2010 1:54 pm
by dunbarx
You can think of a custom property as a sort of global that resides in a control.
The wait command is something that is discouraged in liveCode. The same goes for using the "idle" message. The reason is that "wait" halts all processes while you, er, wait. I do agree that it is easier to understand. But this is really bad practice.
Sending a message at short intervals is much more robust. It does not interfere with any other workings of the stack. Imagine you could, invisibly and in the background, click a button with a script 1000 times per second to check on the status of a property (or variable). This is what is happening when you set up "send message in time".
Now the message is not sent when a script is running, as this takes precedence. But if it was required that the status be checked while that was happening, then some code would have to be included in every instance of every script that might create the condition we are looking for in the first place. I wonder if there is a way to do that?
Craig Newman
Re: disable button until condition
Posted: Fri Sep 24, 2010 3:02 pm
by deebee
Apparently the dev environment doesn't like the wait method at all! I had put it into a script that wasn't yet capable if filling all my variables yet, and LC came screeching to a halt! LC stoppped responding and Windows cut it off several times. Luckily the message box eventually responded and I was able to use it to fill the last variable and comment out the code, then LC took off like a rocket like nothing happened!
It took about an hour altogether to get the messagebox to respond and I was fearful I might have had to trash my whole project!
I will give the custom property a go later. Right now I just have some variable checking going on for each click, and if all are not full, it just throws an error dialog telling the end user what they forgot. A very acceptable solution for me for now!
Re: disable button until condition
Posted: Fri Sep 24, 2010 4:32 pm
by Klaus
Hi Dennis,
OK, I will basically suggest something like dunbarx.
It is always a good idea to explain the problem and logics that will lead to the solution to yourself in plain english!
Thanks to LiveCode you will be able to script it then 1:1!
Here we go:
1. We want to check for a condition periodically until the condition is true.
2. So when we check the condition and it is true, we enable that button (in your case) and are done
3. If the condition is NOT true, we check again after a certain amount of time
4. etc. until the conditon is true or the world stops spinning or the user quits the app
Now we script this (in the stack script):
1.
Code: Select all
on openstack
global XYZ
disable btn X
check4condition
end openstack
2. We actually check here:
Code: Select all
command check4condition
global XYZ
if XYZ <> empty then
enable btn X
ELSE ## = 3 and 4
send "check4condition" to me in 10 seconds
## or whatever seems to be a good interval
end if
end check4condition
As you can see, we almost tranlated step 1 to 4 1:1.
I strongly recommend this approach and NOT only to newbies!
Another example of this approach is on my website "simple_memory 1", find it here a little down the page:
http://www.major-k.de/xtalke.html
Hope this helps.
Best from germany
Klaus
Re: disable button until condition
Posted: Fri Sep 24, 2010 5:08 pm
by deebee
OK, I see now. I was getting "
function" and "
command" mixed up for starters. I'm not at my LiveCode computer ATM so I can't check it. If I'm understanding correctly the following
should work:
Code: Select all
global gVar1, gVar2
on OpenStack
put empty into gVar1
put empty into gVar2
set the disabled of button "button" to true
waitButton
end OpenStack
command waitButton
if ( (gVar1 is not empty) and (gVar2 is not empty) ) then
set the disabled of button "button" to false
else
send "waitButton" to me in 25 milliseconds
end if
end waitButton
Does it look like I'm thinking clearer? I will try this when I get home tonight!
Re: disable button until condition
Posted: Fri Sep 24, 2010 5:23 pm
by Klaus
Hi Dennis.,
looks good!
Although I think a 1 second intervall would do
Best
Klaus
Re: disable button until condition
Posted: Fri Sep 24, 2010 5:33 pm
by deebee
Awesome! Thank you all so much for the help!