Help with checkbox

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

ittzy
Posts: 27
Joined: Thu Feb 04, 2016 11:48 pm

Help with checkbox

Post by ittzy » Sun Feb 07, 2016 10:01 pm

Hi im very new to all this and i hopning that someone will be able to help me.

Im trying to use a checkbox to separate 2 options, like if it is checked use this template if not use this. So far im able to do that and it changes color between A and B but now to my problem is that i would like the button to change to a third color if im in A or B.

so in my code now it shows blue and text "vänster if its checked and green and text "mitten" if its not checked, now i want it to be red on each option and with text "ta bort"

hope you understand :oops:


my code looks like this right now:

Code: Select all

on mouseUp
   if the hilite of button "checkbox1" then
   Set the label of me to "vänster"
   set backgroundcolor of me to blue
   put "CG 1-1 ADD 0 namn 1" into myVariable
   put quote after myVariable
   put "<templateData>" after myVariable
   
   put "<componentData id=\" after myVariable
   put quote after myVariable
   // Name of the field in FLASH
   put "namn" after myVariable
   put "\" & quote  after myVariable
   put "><data id=\" after myVariable
   put quote after myVariable
   put "text\" after myVariable
   put quote after myVariable
   put " value=\" after myVariable
   put quote after myVariable
   // Name of the field in LIVECODE
   put uniDecode (the unicodetext of field "namn1", "UTF8") after myVariable
   put "\" after myVariable
   put quote after myVariable
   put " /></componentData>" after myVariable
   
    put "</templateData>" after myVariable
   put quote after myVariable
   write myVariable & format("\r\n") to socket "localhost:5250"
   
   else
 
   Set the label of me to "MITTEN"
   set backgroundcolor of me to green
   put "CG 1-1 ADD 0 namn_mitt 1" into myVariable
   put quote after myVariable
   put "<templateData>" after myVariable
   
      put "<componentData id=\" after myVariable
   put quote after myVariable
   // Name of the field in FLASH
   put "namn" after myVariable
   put "\" & quote  after myVariable
   put "><data id=\" after myVariable
   put quote after myVariable
   put "text\" after myVariable
   put quote after myVariable
   put " value=\" after myVariable
   put quote after myVariable
   // Name of the field in LIVECODE
   put uniDecode (the unicodetext of field "namn1", "UTF8") after myVariable
   put "\" after myVariable
   put quote after myVariable
   put " /></componentData>" after myVariable
   
    put "</templateData>" after myVariable
   put quote after myVariable
   write myVariable & format("\r\n") to socket "localhost:5250"

   end if

end mouseup

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

Re: Help with checkbox

Post by dunbarx » Mon Feb 08, 2016 2:50 am

Hi.

So if the user chooses "A", the backColor goes red. If "B", the backColor turns green. I get that.
i would like the button to change to a third color if im in A or B.
"A" or "B", right? What is the state whereby the backColor would be a third color? Are you saying that if the text is changed, rather than the hilite is changed (as per the above), that the backColor follows that sequence?

In other words, the user can check or uncheck the button, OR change the label?

Craig Newman

ittzy
Posts: 27
Joined: Thu Feb 04, 2016 11:48 pm

Re: Help with checkbox

Post by ittzy » Mon Feb 08, 2016 9:12 am

no it all ready changes color and label between A and B. My problem is that i want it to change like this:

option A if checked: set label "vänster" and backcol to blue and now it shows my template, now i want it to turn red and label "ta bort"

same with option B

option B unchecked: set label "mitten" and backcol green and now it shows my template, now i want it to turn red and label "ta bort"

and all this is in the same button, like i showed in the script in my first post but in that script it just changes between label, text and template i want to add the part where it just turns red and changes the label to "ta bort".

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

Re: Help with checkbox

Post by dunbarx » Mon Feb 08, 2016 3:15 pm

Hmmm.

So it is just that a short delay is what you need?

Code: Select all

on mouseUp
   if the hilite of me then
      set the label of me to "vanster"
       set the backColor of me to "blue"
      --doVansterStuff
   else
        set the label of me to "mitten"
        set the backColor of me to "green"
         --doMittenStuff
     end if
     
     wait 60
      set the label of me to "ta bort"
     set the backColor of me to "red"
--doTaBortStuff?
end mouseUp
Craig

ittzy
Posts: 27
Joined: Thu Feb 04, 2016 11:48 pm

Re: Help with checkbox

Post by ittzy » Mon Feb 08, 2016 5:04 pm

no not really :oops:

maybe its to hard to explain. its like having 2 buttons in one depending on if the checkbox is checked or not.


its possible to make two buttons but it would be so much nicer if its possible to but it in one..


what more info do you need? more code?


so like this, I try again with exempel A checked & B unchecked:
If checked: ( Green button)show video A then turn same button to a remove video A (Red)
If unchecked: (Blue button) show video B then turn the sam button to a remove video B (Red)

all in one button

and no exakt time when it turns red just when i click the button it plays and then turns to red so i can remove like i explain in my exempel.

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Help with checkbox

Post by MaxV » Mon Feb 08, 2016 6:18 pm

Use radio buttons. Group them. See http://livecode.wikia.com/wiki/Radio_button
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: Help with checkbox

Post by dunbarx » Mon Feb 08, 2016 6:46 pm

Hi.

These things are never easy to explain. Try this on a new card with a checkbox and a field. Make sure the autoHilite of the button is 'false":

Code: Select all

on mouseUp
   switch
      case the backColor of me = "red"
         set the backColor of me to ""
         if the hilite of me = "false"  then
            put "VideoBStopped" into fld 1
         else
              put "VideoAStopped" into fld 1
         end if
         exit to top
         break     
      case the hilite of me = "false"
         set the hilite of me to "true"
         set the label of me to "mitten"
         set the backColor of me to "green"
         put "showingVideoA" into fld 1
         break
      case the hilite of me = "true"
         set the hilite of me to "false"
         set the label of me to "vanster"
         set the backColor of me to "blue"
            put "showingVideoB" into fld 1
         break
   end switch
   wait 30
   set the label of me to "ta bort"
   set the backColor of me to "red"
end mouseUp
The current running video is only indicated by the hilite of the button. That is, I am not sure what the "idle" state (nothing playing) should look line. Therefore I was not sure what the label ought to be (the current state? the next state? Do you always play "B" after you play "A"?). I made the backColor empty just to show you that both videos had been stopped. So you may want to modify this a bit.

I think that setting the backColor is a bit heavy-handed; it is hard to read the button text when blue. Why not set something more subtle?

Write back if i am getting even farther away from what you really want. But know that this can be easily done, and once it is, the code will seem simple and logical.

Craig

ittzy
Posts: 27
Joined: Thu Feb 04, 2016 11:48 pm

Re: Help with checkbox

Post by ittzy » Tue Feb 16, 2016 10:59 pm

not really right, the video was just as an example so you would understand I'm going to use Templates that will play in casparCG. The stop button should appear as soon as i play it not 30s after.


is there anywhere to read about using the checkbox?

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

Re: Help with checkbox

Post by dunbarx » Wed Feb 17, 2016 12:11 am

Hi.

Did you try this? The "stop" (ta bort in red) appears right away. The "30" is 30 ticks (0.5 seconds), not 30 seconds, but you can lose the 'wait" line anytime you like, or reduce the value. I only put it there so both of us could watch the process with some feedback.

Obviously I am still not getting it. Originally, you said:
o like this, I try again with exempel A checked & B unchecked:
If checked: ( Green button)show video A then turn same button to a remove video A (Red)
If unchecked: (Blue button) show video B then turn the sam button to a remove video B (Red)
If you run the handler, its various parts are left open for you to fill in with whatever functionality you wish. When I put those text messages into fld 1, they were just examples of an action that would take place within that code section.

Can you break this down even further?

Craig

EDIT:

You have to have some delay to allow the user to see the names "mitten" and "vanster", no? If not, then I truly do not understand your problem.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Help with checkbox

Post by [-hh] » Wed Feb 17, 2016 3:39 am

Hi all,
probably Ittzy wants logically not a two-state-button, like checkbox, but a four-state-button. This is without special tricks like timed events not possible to achieve with a two state button.

Here is a *simple* four-state-button, that has the states

A: colour green, action show video A, go to state 'M from A'
M-coming from A: colour red, action stop video A, go to state B
M-coming from B: colour red, action stop video B, go to state A
B: colour blue, action show video B, go to state 'M from B'

Make a standard button (no radio or check), set autoHilite to false.
Then use the following script.

Code: Select all

local myState="A" -- the start value

on mouseUp
  switch myState
    case "A" -- state 1  -> go to state A->M
      --> insert action: show video A
      set backcolor of me to "255,0,0" -- red
      set label of me to "ABORT video A"
      put "A->M" into myState
      break
    case "A->M" -- state 2  -> go to state B
      --> insert action: stop video A
      set backcolor of me to "0,0,255" -- blue
      set label of me to "Show video B"
      put "B" into myState
      break
    case "B->M" -- state 3  -> go to state A
      --> insert action: stop video B
      set backcolor of me to "0,255,0" -- green
      set label of me to "Show video A"
      put "A" into myState
      break
    case "B" -- state 4  -> go to state B->M
      --> insert action: show video B
      set backcolor of me to "255,0,0" -- red
      set label of me to "ABORT video B"
      put "B->M" into myState
      break
  end switch
end mouseUp
[Remark 1: If you use a check button for that you have also to set the hilite (A: true, B: true, else false). And don't use autohilite, because the hiliteColor is by the engine preferred to the backColor.
Remark 2: If you want to use the current state of the button by other objects, you can check the label or use a custom property for the state.]


This is of course not the only solution. Your obviously working (tricky) timed-solution, Craig, has the small disadvantage to influence the display (by wait).
shiftLock happens

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

Re: Help with checkbox

Post by dunbarx » Wed Feb 17, 2016 7:07 pm

Hermann is right, of course. Much simpler if you make a multi-state switch like he suggests.

Sometimes when you want something to work a certain way, but have to move mountains to do so, it is best to sit back and re-think the problem. This may mean giving up the original idea. In fact, doing so opens up the option of new and adorable objects. Be creative...

Craig

ittzy
Posts: 27
Joined: Thu Feb 04, 2016 11:48 pm

Re: Help with checkbox

Post by ittzy » Sat Feb 20, 2016 12:34 pm

OK thx guys for the help.

I will show you know a shorter version of my button that I use right now. I want to make 2 buttons in 1 button with help of the checkbox.


BUTTEN RIGHT NOW (CODE 1)
I will start with showing how my button look like now

Code: Select all

on mouseUp
   if the label of me is "LEFT" then
      set the label of me to "STOP"
      set backgroundcolor of me to yellow
  --TURN ON
  ## CODE

    else
   
      set the label of me to "LEFT"
      set backgroundcolor of me to green
 --TURN OFF
 ## CODE

   end if

end mouseup
So that was 1 button that i use now.



CHECKBOX BUTTON (CODE 2)
OK, here i will show you how the button will look with the checkbox option,

Code: Select all

on mouseUp
   if the hilite of button "check21" then
   Set the label of me to "LEFT"
   set backgroundcolor of me to green
 -- Use left template
 ## Code
     
   else

   Set the label of me to "MIDLE"
   set backgroundcolor of me to lightblue
 -- Use midle template
 ## Code

   end if

end mouseup

as you see in the first code i showed the button turns to "STOP" when i click and then back to "LEFT" when i click agian.

Now i want help to put this funktion in to my second code (CODE 2) that i showed you. Right now that code just changes between "LEFT" and "MIDLE". So Help me pls add the "STOP" line.


I hope this explains everything better, or i will make a clip to explaine hehe

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

Re: Help with checkbox

Post by dunbarx » Sun Feb 21, 2016 10:01 pm

Ittzy.

We will work this out, but I need to know what you mean.

A checkbox has two states, hilited or not hilited, so what is the nature of a third state that would set that third label"? There are lots of ways to do this, of course, three different backColors, as one example. Set the backColor of your button "check21" to red. In the other button:

Code: Select all

on mouseUp
    switch the backColor of btn "check21"
      case "red"
         set the backColor of me to "red"
         set the backColor of btn "check21" to "green"
         break
      case "green"
         set the backColor of me to "green"
         set the backColor of btn "check21" to "blue"
         break
      case "blue"
         set the backColor of me to "blue"
         set the backColor of btn "check21" to "red"
         break
   end switch
end mouseup
This bit of silliness, where the backColor of the working button chases the backColor of button "check21", is only to show that if you want three states, you cannot use the hilite property, or any other that has only two. No checkBoxes. Is this sort of what you are asking?

If not, you need, or rather I need, to have a short flowchart made. This can be entirely in words. And know that we might roll our own "checkBox" that could have three different images, or labels, or icons, or whatever. It's the "three" that matters here, however implemented.

Craig

ittzy
Posts: 27
Joined: Thu Feb 04, 2016 11:48 pm

Re: Help with checkbox

Post by ittzy » Mon Feb 22, 2016 11:47 pm

hmm its not exactly that i mean, i don't really know how to explain better then my last post. I will show 4 pictures to you maybe that help.


This is how the button looks right now and when you klick that green button it shows a Middle Template (like I wrote in the code in my thread).
Image

And as soon as you klicked that green button it plays the Template and turns to this.
Image
And when i click that same red button it removes my template.

That works perfectly.





Now i will show this picture that is the whole problem.
Image
Image


So i have explained how the button works and that works good but it only shows me my "middle Template" and i want to make a function where i want to use the checkbox to the right as an option to show me a "Left Template", So if the Checkbox is marked the same button will show Left Template if its not checked like in the first picture it plays the Middle Template.

ALL THIS PICTURES ARE THE SAME BUTTON AND LINES

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

Re: Help with checkbox

Post by dunbarx » Tue Feb 23, 2016 12:21 am

Hi.

OK. Now we have more than two states.

Try this. On a new card, make a field, a standard button and a checkBox. Name the checkBox "Vanster". Before you start. from the message box, set the backColor of the standard button to "green". Make sure its "opaque" property is "true".

Put this in the standard button script:

Code: Select all

on mouseUp
   switch 
      case the backColor of me = "green" and not the hilite of btn "vanster"
         set the backColor of me to "red"
         set the label of me to "Ta Bort"
         put "Middle" into fld 1
         break
      case the backColor of me = "green" and the hilite of btn "vanster"
         set the backColor of me to "red"
         set the label of me to "Ta Bort"
         put "Left" into fld 1
         break
      case the backColor of me = "red" 
         set the backColor of me to "green"
            set the label of me to "Visa"
         set the hilite of btn "vanster" to "false"
         put "" into fld 1
         break
   end switch
end mouseUp
Please tell me if this is what you had in mind. You have to adapt this to your "template" gadgets, I only put a word into a field.

Craig

Post Reply