Switch button

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

Post Reply
cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Switch button

Post by cougar05 » Fri May 08, 2020 3:32 pm

Hello

I'm a really newbie and my english is also really not good! Sry for that!

I play a little bit with LiveCode and i have a problem with a switch button.

In my script i set a normal button that should set by klicking the switch button from on to off. The button also put empty into some fields.
I have really no idea how can i fix that.

Pleas help me!

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Switch button

Post by Klaus » Fri May 08, 2020 3:46 pm

Hi cougar05,

welcome to the forum!

The "Switch button" widget behaves like a checkbox or radiobutton, so you can just:

Code: Select all

...
##Switch OFFF
set the hilite of widget "your switch here" to FALSE
...
## Switch ON
set the hilite of widget "your switch here" to TRUE
...
Please help me!
That is what this forum is made for! :D


Best

Klaus

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 4:05 pm

Hello Klaus!

Thanks a lot for your answer!!!
It helps me a lot!

I put this code into the script of the button "reset"

on mouseUp
put empty into fld "field1"
put empty into fld "field2"
put empty into fld "field3"
set the hilite of widget "switch1" to FALSE
set the hilite of widget "switch2" to FALSE
set the hilite of widget "switch3" to FALSE
end mouseUp

It works perfectly! Thank you Klaus!

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 4:18 pm

Can you help me with an oder problem in this script?

I want to write a little script for a shoppinglist.
A textfield left on right a switch. When the switch is on, then the text in the field goes strikeout.
On the button a reset button (that works)

The script in the switches is:

on hiliteChanged
if the textstyle of fld "field1" is "strikeout" then
set the textstyle of fld "field1" to "normal"
else
set the textstyle of fld "field1" to "strikeout"
end if
end hiliteChanged

But sometime when i run the programm and i write a text in the field. the text is strikeout from the beginning. Is there a bug in the sript?

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Switch button

Post by Klaus » Fri May 08, 2020 4:36 pm

Hi cougar05,

what country are you from?

The problem with your script is, there is no NORMAL textstyle in LC, you mean PLAIN!

Code: Select all

on hiliteChanged
 if the textstyle of fld "field1" is "strikeout" then
   set the textstyle of fld "field1" to "plain"
 else
   set the textstyle of fld "field1" to "strikeout"
 end if 
end hiliteChanged
That should do the trick.

Hint:
After pasting your code here, select it again and click on the CODE button on top of this text entry field.
The one that reads: </>
This way the formatting will be kept intact, see my script!

To learn more about the very basics of LC, check the stacks here:
http://www.hyperactivesw.com/revscriptc ... ences.html
And keep asking! :-)


Best

Klaus

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 4:46 pm

Wie ich sehe bist du aus Deutschland. Ich bin aus Österreich!
Vielen Dank für die Info!
Die Seite ist auch sehr hilfreich.

Leider ist es beim Script noch immer so, dass das oberste Feld mit dem Switch daneben immer durchgestrichen beginnt. Erst wenn ich den Switch "einschalte" wird der Text "plain"

LG, Manuel

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Switch button

Post by Klaus » Fri May 08, 2020 4:56 pm

Hi Manuel,

we also have a german LC forum: http://www.livecode-blog.de/forums/foru ... ode-forum/
So please continue in english!

Hm, sounds like that LAST formatting of the field is being saved with the stack.
So add this to the "on openstack" handler, so the field is set to PLAIN when the stack opens:

Code: Select all

on openstack
  set the textstyle of fld "field1" of cd 1 to "plain"
  ## More openstack stuff here...
end openstack
Hint:
You can put this into the stackscript or the script of the first card of your stack.


Best

Klaus

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 5:24 pm

Thank you for the info Klaus!
But the problem is still there.
When i save the program and open it new and write something, klick on the switch to strikeout and klick again to plain ist. And when i klick the reset button and write something in one of the fields, its strikeout. Even though the switch is off ;(

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Switch button

Post by Klaus » Fri May 08, 2020 5:33 pm

Hm, very mysterious! :shock:

But wait, what does your "reset" button do?
Please post the script of that button.

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 7:24 pm

I fixed it!
Thanks for your help!

This is in the script from the reset button:

Code: Select all

on mouseUp

put empty into fld "field1"

put empty into fld "field2"

put empty into fld "field3"

put empty into fld "field4"

put empty into fld "field5"

put empty into fld "field6"

put empty into fld "field7"

put empty into fld "field8"

put empty into fld "field9"

set the hilite of widget "switch1" to FALSE

set the hilite of widget "switch2" to FALSE

set the hilite of widget "switch3" to FALSE

set the hilite of widget "switch4" to FALSE

set the hilite of widget "switch5" to FALSE

set the hilite of widget "switch6" to FALSE

set the hilite of widget "switch7" to FALSE

set the hilite of widget "switch8" to FALSE

set the hilite of widget "switch9" to FALSE

set the textstyle of fld "field1" of cd 1 to "plain"

set the textstyle of fld "field2" of cd 1 to "plain"

set the textstyle of fld "field3" of cd 1 to "plain"

set the textstyle of fld "field4" of cd 1 to "plain"

set the textstyle of fld "field5" of cd 1 to "plain"

set the textstyle of fld "field6" of cd 1 to "plain"

set the textstyle of fld "field7" of cd 1 to "plain"

set the textstyle of fld "field8" of cd 1 to "plain"

set the textstyle of fld "field9" of cd 1 to "plain"

end mouseUp

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Switch button

Post by Klaus » Fri May 08, 2020 7:33 pm

How did you fix it? :shock:

Hint:
When in the script editor, hit the TAB key from time to time, then LC will format the script with indents etc.
THEN you can copy the scipt, paste it here and format it as CODE.

Hint:
You can save some typing, this will do the same with a REPEAT Loop:

Code: Select all

on mouseUp
   repeat with i = 1 to 9
      set the hilite of widget ("switch" & i) to FALSE
      put empty into fld ("field" & i)
      set the textstyle of fld ("field" & i) to "plain"  
   end repeat
end mouseUp
LC will first resolve the thing in parens (Klammern) so it knows the exact name of the controls to process.
In the first loop i has the value of 1 so it resolves to:
("switch" & i) -> switch1
("field" & i) -> field1
etc.


Best

Klaus

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 7:42 pm

The line "set the hilite of widget "switch1" to FALSE" you told me to put in the Stack Script. I put it in the reset button script to. Now it works :D

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Switch button

Post by Klaus » Fri May 08, 2020 8:08 pm

cougar05 wrote:
Fri May 08, 2020 7:42 pm
The line "set the hilite of widget "switch1" to FALSE" you told me to put in the Stack Script.
I never did that! :shock:
THIS was my advice, Sir:

Code: Select all

on openstack
  set the textstyle of fld "field1" of cd 1 to "plain"
  ## More openstack stuff here...
end openstack

cougar05
Posts: 8
Joined: Fri May 08, 2020 3:25 pm

Re: Switch button

Post by cougar05 » Fri May 08, 2020 8:36 pm

:lol: :lol: :lol: My bad!

In the end, your advice work at all!
Thanks a lot!!!

Post Reply