Radio buttons to show/hide group

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Kevtor
Posts: 15
Joined: Tue Jul 02, 2013 11:51 am

Radio buttons to show/hide group

Post by Kevtor » Fri Oct 10, 2014 11:25 am

Hi

I have a group of radio buttons and I have a group of data for each button.
When one button is pressed I want to show it's data group and hide the previous button's group of data.
The showing part is no problem, it's the hiding I can't figure out.
Can someone please point me in the right direction?

Thanks,
Kev

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Radio buttons to show/hide group

Post by dave.kilroy » Fri Oct 10, 2014 11:32 am

Hi Kevtor and welcome to the forum!

How about something like the following code in the script of your group containing your option buttons?

Code: Select all

on mouseUp
   switch
      case the hilite of btn 1 of me
         show grp "Data1"
         hide grp "Data2"
         hide grp "Data3"
         break
      case the hilite of btn 2 of me
         hide grp "Data1"
         show grp "Data2"
         hide grp "Data3"
         break
      case the hilite of btn 3 of me
         hide grp "Data1"
         hide grp "Data2"
         show grp "Data3"
         break
   end switch
end mouseup
Dave

PS: I edited my answer to make it more specific to your case - hope it helps...
"...this is not the code you are looking for..."

Kevtor
Posts: 15
Joined: Tue Jul 02, 2013 11:51 am

Re: Radio buttons to show/hide group

Post by Kevtor » Fri Oct 10, 2014 12:16 pm

Thanks Dave,

Some cutting and pasting, switching shows and hides, changing of group names and it worked like a charm.

Thanks
Kev

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Radio buttons to show/hide group

Post by dave.kilroy » Fri Oct 10, 2014 2:08 pm

Good :)

Of course "there must be 50 ways" to show/hide your groups with LiveCode and others may chime in with better approaches...
"...this is not the code you are looking for..."

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

Re: Radio buttons to show/hide group

Post by dunbarx » Fri Oct 10, 2014 2:23 pm

Dave.

If the OP has locally explicit show/hide needs, then your approach, essentially a "look-up" table tailored for each case instance, is probably the best.

There is a compulsion among some (like me) to try to condense these sorts of tasks with, say, repeat loops and ordered object references (like "group1", "group2", etc), but often a brute force method is simplest, and easiest to maintain.

Craig Newman

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Radio buttons to show/hide group

Post by dave.kilroy » Fri Oct 10, 2014 2:40 pm

Craig

I will admit to considerable 'brute force' tendencies in my code but am not without SOME refactoring tendencies :)

You recently took part in this conversation: http://runtime-revolution.278305.n4.nab ... l#a4684359 where Geoff wrote my favourite coding aphorism of recent times:
Premature optimization is the root of <some> evil...
Kind regards

Dave
"...this is not the code you are looking for..."

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Radio buttons to show/hide group

Post by jacque » Fri Oct 10, 2014 4:29 pm

I generally use a repeat loop and just hide everything. After the loop completes I show the object I want. This works for any number of objects, uses a small amount of code (one line inside the loop) and doesn't require any customization. You can add new objects to the group later and it still works without changes.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Radio buttons to show/hide group

Post by dave.kilroy » Fri Oct 10, 2014 4:32 pm

very neat! :)
"...this is not the code you are looking for..."

Kevtor
Posts: 15
Joined: Tue Jul 02, 2013 11:51 am

Re: Radio buttons to show/hide group

Post by Kevtor » Fri Oct 10, 2014 8:22 pm

Thanks for the replies everyone.

I tried to do a repeat but I was trying to save names to variables and couldn't get it to work.
It works for now using switch/case so I can move on to other parts of the project but I intend to come back to it and try Jaque's method for the heck of it.
With 14 buttons the script ended up quit wordy, heh.

Thanks,
Kev

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: Radio buttons to show/hide group

Post by keram » Sat Oct 11, 2014 9:57 am

I was playing with this little bit and came up with:

Code: Select all

on mouseUp
   repeat with x = 2  to the number of grps of this cd -- start with grp 2 since grp 1 is the radiobuttons
      hide grp x
   end repeat 
   put label of target into tNr; show grp (tNr +1)
end mouseUp
see the attached stack

Maybe that's what Jacque meant?
Attachments
show-hide groups.zip
(1.11 KiB) Downloaded 249 times
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

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

Re: Radio buttons to show/hide group

Post by dunbarx » Sat Oct 11, 2014 4:30 pm

Hi.

Exactly

What I mentioned) earlier in the thread was more like this. Say you have groups named "grp1, grp2,grp3, otherGrp1, otherGrp2, otherGrp3, etc. I mentioned a loop that referenced some property of those groups, in this case, their names:

Code: Select all

on mouseUp
  repeat with y = 1 to the number of groups
    hide grp "otherGrp" & y
  end repeat
end mouseUp
See? This will hide only those groups where their names resolve to the "otherGrp", er group, and hide them. More important, do you see how the simple concatenation works?

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Radio buttons to show/hide group

Post by jacque » Sat Oct 11, 2014 5:35 pm

Yes, that's what I meant.

I'd recommend not using semicolons to concatenate two lines of code. One person in the forums does that but it's nonstandard and is likely to cause more work for the parser. But mainly it makes your code harder to read.

Another pet peeve of mine is the omission of the word "rhe" for properties. This has taken off here in the forums after a few people began writing scripts that way. It forces the parser to do much more work to determine whether the word is a variable or a property name.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Radio buttons to show/hide group

Post by dave.kilroy » Sat Oct 11, 2014 6:22 pm

Jacque - that's interesting about the cost of dropping the "the" in properties - the dictionary doesn't mention this
The the keyword is optional before the names of properties, but it is good style to include it.
Any other 'bad style' practices that lead to slow-downs spring to mind?
"...this is not the code you are looking for..."

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

Re: Radio buttons to show/hide group

Post by dunbarx » Sat Oct 11, 2014 6:30 pm

What Jacque meant, and she too much of a lady to say it, was this:

It is TERRIBLE to append multiple lines of code that way. I am perplexed why the feature is supported at all. I think it was proposed by liberal Hollywood A-list actors.

Lines are cheap. Readability is not.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Radio buttons to show/hide group

Post by jacque » Sat Oct 11, 2014 7:22 pm

Dave, good catch. I wasn't aware that the dictionary said that. That may explain why so many have adopted the style.

Perhaps it isn't as awful as I thought and my reaction is just me being a purist. But logically I think it would produce the same type of slowdown as unquoted literals where the engine has to work out what you meant. If I weren't so lazy I'd do a timing test.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply