Page 1 of 3
Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 11:25 am
by Kevtor
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
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 11:32 am
by dave.kilroy
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...
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 12:16 pm
by Kevtor
Thanks Dave,
Some cutting and pasting, switching shows and hides, changing of group names and it worked like a charm.
Thanks
Kev
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 2:08 pm
by dave.kilroy
Good
Of course "there must be 50 ways" to show/hide your groups with LiveCode and others may chime in with better approaches...
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 2:23 pm
by dunbarx
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
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 2:40 pm
by dave.kilroy
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
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 4:29 pm
by jacque
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.
Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 4:32 pm
by dave.kilroy
very neat!

Re: Radio buttons to show/hide group
Posted: Fri Oct 10, 2014 8:22 pm
by Kevtor
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
Re: Radio buttons to show/hide group
Posted: Sat Oct 11, 2014 9:57 am
by keram
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?
Re: Radio buttons to show/hide group
Posted: Sat Oct 11, 2014 4:30 pm
by dunbarx
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
Re: Radio buttons to show/hide group
Posted: Sat Oct 11, 2014 5:35 pm
by jacque
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.
Re: Radio buttons to show/hide group
Posted: Sat Oct 11, 2014 6:22 pm
by dave.kilroy
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?
Re: Radio buttons to show/hide group
Posted: Sat Oct 11, 2014 6:30 pm
by dunbarx
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
Re: Radio buttons to show/hide group
Posted: Sat Oct 11, 2014 7:22 pm
by jacque
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.