Previous next buttons

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, asayd

Previous next buttons

Postby chris25 » Wed Oct 16, 2013 5:20 pm

Screen shot 2013-10-16 at 17.08.36.png
Somewhat terribly confused here, while I fully understand both the concept and the scripts themselves I fail to see why he has two separate scripts in two separate tabs with two completely different names. What I mean is this, the tab labelled " 'card prevNextButtons' " is written at the Stack level - I checked. But I can not find where tab labelled 'card commands' is written (by the way this whole thing is a group); anyway, I don't understand why he could not have written the script for 'prev' and 'next' within the same tab, same page so to speak. When you open the two scripts by pressing on the "open script of first card" you get the tab - "prevnextButtons" tab; when you open the last card button you get the "cardCommands" tab. Why?
chris25
 
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Previous next buttons

Postby Mark » Wed Nov 13, 2013 11:47 am

Hi,

There are two cards in this stack. Both cards have a script. There is no relevant script at stack level, unless a script in one of the two cards calls a script at stack level, but I don't think this is a the case. Therefore, I don't think that your observation is correct. Something must have confused you ;-)

The first card handles the next button. The second card handles the Prev button. (Apparently, the first card is called "Commands" and the second card is called "PrvNextButtons" but they could have any name you like). Therefore, you need two different sets of scripts and it makes sense to write these scripts at card level rather than stack level.

The two navigation buttons, and possibly all other controls too, are in a group because you need them on both cards. It is obvious.

The script of cd 1:

Code: Select all
on openCard
  enable btn "Next"
end openCard

on closeCard
  disable btn "Next"
end closeCard


The script of cd 2:

Code: Select all
on openCard
  enable btn "Prev"
end openCard

on closeCard
  disable btn "Prev"
end closeCard



The first card enables and disables the "Next" button. The second card enables and disables the "Prev" button. This makes sense.

If you have mutliple cards, it doesn't make sense to write these scripts at card level, because you don't want to have a script in every card if you can avoid it. The following script, written at stack level, is a much better alternative:

Code: Select all
on preOpenCard
  if the number of this cd is 1 then
    disable btn "Prev"
    enable btn "Next"
  else if the number of this cd is the number of cards then
    enable btn "Prev"
    disable btn "Next"
  else
    enable btn "Prev"
    disable btn "Next"
  end if
end preOpenCard


This last example is better, because you don't need to make sure that the first card always stays the first card and the last card always stays the last card. You can shuffle the cards at will and the script will still work correctly.

Kind regards,

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/livecodebook
Mark
Livecode Opensource Backer
Livecode Opensource Backer
 
Posts: 5118
Joined: Thu Feb 23, 2006 9:24 pm

Re: Previous next buttons

Postby chris25 » Fri Nov 15, 2013 10:11 pm

Hi Mark, I have just come here out of curiosity and noticed with delight that you answered these questions. Actually that is great about writing one script at stack level. Thanks.
chris25
 
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Previous next buttons

Postby Mark » Fri Nov 15, 2013 10:25 pm

You're welcome :-)
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/livecodebook
Mark
Livecode Opensource Backer
Livecode Opensource Backer
 
Posts: 5118
Joined: Thu Feb 23, 2006 9:24 pm

Re: Previous next buttons

Postby Klaus » Sun Nov 17, 2013 1:10 pm

Hi guys,

what about this cool (but maybe a tad mentally challenging) solution using BOOLEan values? :D
Monsieur Boole can avoid a lot of unneccessary IF...THEN clauses!
Code: Select all
on preOpenCard
  put (the num of this cd = 1) into isFirstCD
  ## TRUE if we are on the first card, otherwise FALSE!

  put (the num of this cd = the num of cds) into isLastCD
  ## TRUE if we are on the last card, otherwise FALSE!

  ## Now simply use these values in a clever way. Hey, in programming its all about logics :-)
  set the DISABLED of btn "Prev" isFirstCD
  set the DISABLED of btn "Next" isLastCD
end preOpenCard

OK, after you have digested this, you will see the benefits of using
BOOLEan values in situations like these.

I put the values into variables first, but we could skip this step and simply write:
Code: Select all
...
set the DISABLED of btn "Prev" to (the num of this cd = 1)
set the DISABLED of btn "Next" to (the num of this cd = the num of cds)
...

Et voila, a clean 2 liner!

Have fun :D


Best

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


Return to LiveCode University

Who is online

Users browsing this forum: No registered users and 2 guests