SWAP bettwen 2 buttons

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

pajito
Posts: 30
Joined: Thu Apr 13, 2017 8:08 pm

SWAP bettwen 2 buttons

Post by pajito » Fri May 19, 2017 2:43 pm

Hi everybody

Probaby my problem is quite easy for the majority of you but still it is a problem for me.
I have 2 sets of buttons or 2 rows of buttons if you prefer.
PRV1 PRV2 PRV3
PGM1 PGM2 PGM3
Only ONE button from each row can be pressed every time.
Also there is also a button called TRANS.
I need by pressing the TRANS button to swap the mouseUps betwen the 2 rows. For example If PRV1 and PGM3 were pressed, I need to send mouseUp to PGM1 and PRV3.

bellow is the code from one button from PGM row, it is the same for every other button

Code: Select all


global gVarPgm
on mouseUp
   
   put "PGM2" into gVarPgm
   set the textcolor of me to "yellow"
   set the textcolor of button "PGM1"  to "BLACK"
   set the textcolor of button "PGM3"  to "BLACK"
   set the backgroundcolor of me to "RED"
   set the backgroundcolor of button "PGM1"  to "WHite"
   set the backgroundcolor of button "PGM3"  to "WHite"
   
end mouseUp

bellow is the code from one button from PRV row,

Code: Select all


on mouseUp
   global gVarPrv
   put "prv3" into gVarPrv
   
   set the backgroundcolor of me to "green"
   set the backgroundcolor of button "PRV1"  to "WHite"
   set the backgroundcolor of button "PRV2"  to "WHite"
   
end mouseUp



bellow is the code from TRANS button

Code: Select all

global gVarPrv 
global gVarPgm
on mouseUp
   
   
  
   set the backgroundcolor of me to "YELLOW"
   
   if gVarPrv = "prv1"
      then send "mouseUp" to button "PGM1"
   else if  gVarPrv = "prv2"
   then send "mouseUp" to button "PGM2"
   else if gVarPrv = "prv3"
   then send "mouseUp" to button "PGM3"

 if gVarPrv = "pgm1"
      then send "mouseUp" to button "PRV1"
   else if  gVarPrv = "pgm2"
   then send "mouseUp" to button "PRV2"
   else if gVarPrv = "pgm3"
   then send "mouseUp" to button "PRV3"
   




end mouseUp


my main problem is that when I press the TRANS button the gVarPGM value is changed, so the second part of the code of TRANS button is useless.

Thank You all in advance

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

Re: SWAP bettwen 2 buttons

Post by Klaus » Fri May 19, 2017 4:51 pm

Hiola pajito,

not sure I understand your problem, but looks like the ORDER of your commands is important
(first let the button "TRANS" do its work and THEN change the value of your global), so maybe
this will work for you?

Code: Select all

global gVarPrv 
global gVarPgm

on mouseUp   
   set the backgroundcolor of me to "YELLOW"
   
   switch gVarPrv
      case "prv1"
         put "PGM1" into tButton
         break
      case "prv2"
         put "PGM2" into tButton
         break
      case "prv3"
         put "PGM3" into tButton
         break
      case "pgm1"
         put "PRV1" into tButton
         break
      case "pgm2"
         put "PRV2" into tButton
         break
      case "pgm3"
         put "PRV3" into tButton
         break
   end switch
   send "mouseup" to btn tButton   
end mouseUp
Please note my use of SWITCH which makes the script a lot more readable.


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: SWAP bettwen 2 buttons

Post by dunbarx » Fri May 19, 2017 4:54 pm

Hi.

You do not need all that. Take a moment and make a new card. Make your three "PRV" buttons, and group them. Now make the three "PMG" buttons and group them. Now make the button "Trans", and put this into its script:

Code: Select all

on mouseUp
   put the hilitedButton of grp 1 into a
   put the hilitedButton of grp 2 into b
   set the hilitedButton of grp 1 to b
   set the hilitedButton of grp 2 to a
end mouseUp
Think about this handler and write back.

Craig Newman

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

Re: SWAP bettwen 2 buttons

Post by Klaus » Fri May 19, 2017 5:32 pm

Hi Craig,

this will only work with RADIO buttons, and pajito did not mention this.
Besides that, did you take a look at his script of btn "TRANS"?


Best

Klaus

pajito
Posts: 30
Joined: Thu Apr 13, 2017 8:08 pm

Re: SWAP bettwen 2 buttons

Post by pajito » Fri May 19, 2017 6:05 pm

dunbarx wrote:Hi.

You do not need all that. Take a moment and make a new card. Make your three "PRV" buttons, and group them. Now make the three "PMG" buttons and group them. Now make the button "Trans", and put this into its script:

Code: Select all

on mouseUp
   put the hilitedButton of grp 1 into a
   put the hilitedButton of grp 2 into b
   set the hilitedButton of grp 1 to b
   set the hilitedButton of grp 2 to a
end mouseUp
Think about this handler and write back.

Craig Newman
Hi Craig unfortunatelly Klaus is right, the trick works, but only keeps highliting the buttons. I tried to add the following but it gets into a loop of highlighting the buttons until crash point.
I have to thank you anyway, you gave me something to think about.

Code: Select all

 send mouseUp into the hilitedButton of grp 1
   send mouseUp into the hilitedButton of grp 2

pajito
Posts: 30
Joined: Thu Apr 13, 2017 8:08 pm

Re: SWAP bettwen 2 buttons

Post by pajito » Fri May 19, 2017 6:10 pm

Klaus wrote:Hiola pajito,

not sure I understand your problem, but looks like the ORDER of your commands is important
(first let the button "TRANS" do its work and THEN change the value of your global), so maybe
this will work for you?

Code: Select all

global gVarPrv 
global gVarPgm

on mouseUp   
   set the backgroundcolor of me to "YELLOW"
   
   switch gVarPrv
      case "prv1"
         put "PGM1" into tButton
         break
      case "prv2"
         put "PGM2" into tButton
         break
      case "prv3"
         put "PGM3" into tButton
         break
      case "pgm1"
         put "PRV1" into tButton
         break
      case "pgm2"
         put "PRV2" into tButton
         break
      case "pgm3"
         put "PRV3" into tButton
         break
   end switch
   send "mouseup" to btn tButton   
end mouseUp
Please note my use of SWITCH which makes the script a lot more readable.


Best

Klaus



Hi Klaus,
probably I didn't made my problem clear. The problem is that even with your advance code, if the first switch works it changes the second case before examing it. After all I need to use, the previous "input" of PGM set of buttons.
Thanks for everything

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: SWAP bettwen 2 buttons

Post by dunbarx » Fri May 19, 2017 6:49 pm

@Klaus.
this will only work with RADIO buttons,
Who says so? 8)

@Pajito. So it is true that I did nothing more than hilite. But the handler can be expanded to set the properties you wish as well. I just wanted you to understand the internal properties of grouped controls, the hilite being one of the best of them, and peculiar to groups. Can you see your way forward to including the other features as well.

Craig

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

Re: SWAP bettwen 2 buttons

Post by Klaus » Fri May 19, 2017 6:54 pm

dunbarx wrote:@Klaus.
this will only work with RADIO buttons,
Who says so? 8)
I do! :D

This just does not work with "normal" pushbuttons!

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

Re: SWAP bettwen 2 buttons

Post by Klaus » Fri May 19, 2017 6:58 pm

Hola pajito,
pajito wrote: Hi Craig unfortunatelly Klaus is right, the trick works, but only keeps highliting the buttons. I tried to add the following but it gets into a loop of highlighting the buttons until crash point.
I have to thank you anyway, you gave me something to think about.

Code: Select all

 send mouseUp into the hilitedButton of grp 1
   send mouseUp into the hilitedButton of grp 2
so you ARE using radiobuttons?
If yes, the this will work:
...
send "mouseUp" to btn (the hilitedButtonname of grp 1)
send "mouseUp" to btn (the hilitedButtonname of grp 2)
...

Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: SWAP bettwen 2 buttons

Post by dunbarx » Fri May 19, 2017 7:24 pm

Klaus.

Yes it does. It works with any style of button. Try the gadget I suggested.

To be fair, I did not know this beforehand...

Craig

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

Re: SWAP bettwen 2 buttons

Post by Klaus » Fri May 19, 2017 7:34 pm

Hi Craig,

does not work with LC 9 dp6 (and did not since the Metacard days)
See attached screenshot, script of btn "Button" -> results in 0

Best

Klaus
Bildschirmfoto 2017-05-19 um 20.32.29.jpg

mrcoollion
Posts: 719
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: SWAP bettwen 2 buttons

Post by mrcoollion » Fri May 19, 2017 8:20 pm

Just an idea...

Make use of segmented control widgets
step 1) add 2 segmented control widgets on the card and name them resp "ButtonRow1" and "ButtonRow2"
step 2) add a button TRANS with the code below.

Select an option on each button row then press button TRANS.
(Be aware that the widget initially has no highlited item, i did not code for this situation).

Code: Select all

on mouseUp
    put the hilitedItems of widget "ButtonRow1" into tRow1
    put the hilitedItems of widget "ButtonRow2" into tRow2
    answer "Ok now switch" 
    set the hilitedItems of widget "ButtonRow1" to tRow2
    set the hilitedItems of widget "ButtonRow2" to tRow1
end mouseUp

Friendly regards,

Paul

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: SWAP bettwen 2 buttons

Post by jacque » Fri May 19, 2017 8:26 pm

Group the PGM buttons and name the group "PGM". Group the PRV buttons and name the group "PRV".

Script of group "PGM":

Code: Select all

on mouseUp
  repeat with x = 1 to the number of btns of me
    set textcolor of btn x of me to "black"
    set the backcolor of btn x of me to "white"
  end repeat
  set textcolor of the target to "yellow"
  set the backcolor of the target to "red"
end mouseUp
Script of group "PRV":

Code: Select all

on mouseUp
  repeat with x = 1 to the number of btns of me
    set the backcolor of btn x of me to "white"
  end repeat
  set the backcolor of the target to "green"
end mouseUp
Script of button "trans":

Code: Select all

on mouseUp
  set the backgroundcolor of me to "YELLOW"
  -- get the currently active btn in each grp:
  repeat with x = 1 to the number of btns in grp "PGM"
    if the backcolor of btn x of grp "PGM" = "red" then
      put x into tPGMBtn
      exit repeat
    end if
  end repeat
  repeat with x = 1 to the number of btns in grp "PRV"
    if the backcolor of btn x of grp "PRV" = "green" then
      put x into tPRVBtn
      exit repeat
    end if
  end repeat
  -- reverse swap:
  send "mouseUp" to btn tPGMBtn of grp "PRV"
  send "mouseUp" to btn tPRVBtn of grp "PGM"
end mouseUp
Is that what you need to happen? This method doesn't require any global variables.

Klaus is right, radioBehavior only works on radio buttons, at least in newer versions of LC, even though the property is available in the inspector. Craig, what button type are you using? I only checked rectangle buttons, but historically the behavior was named "radioBehavior" for a reason. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: SWAP bettwen 2 buttons

Post by dunbarx » Fri May 19, 2017 8:51 pm

Jacque, Klaus.

All types, in v.6.7.

Odd that this would have changed in later versions, since hardly anyone even knew about it.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: SWAP bettwen 2 buttons

Post by jacque » Fri May 19, 2017 10:44 pm

I vaguely recall radioBehavior working like that at some point but it isn't/wasn't standard and was likely a useful bug. It's been removed again.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”