Radio Buttons

Post Reply
User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Radio Buttons

Post by rbytes » Mon Sep 17, 2018 11:52 pm

Handy code for creating a set of buttons where pressing any one cancels the others.

Code: Select all

/*
Radio Buttons
by rbytes, September 2018
Each button, when selected,
cancels the others
*/
SET BUTTONS CUSTOM
SET TOOLBAR OFF
GET SCREEN SIZE sw,sh
rw=sw/1024!rh=sh/724
GRAPHICS
GRAPHICS CLEAR
SET BUTTONS FONT SIZE 30*rw
FILL ALPHA 0
DRAW COLOR 0,0,0

' make fields
FIELD "black" TEXT "" AT 90*rw-2,278*rh-2 SIZE 610*rw+4,40*rh+4 RO   ' black border
FIELD "black" BACK COLOR 0,0,0
FIELD "bg" TEXT " Background:  Clear          White          Black             Red             Blue" AT 90*rw,278*rh SIZE 610*rw,40*rh RO    ' button labels
FIELD "bg" FONT SIZE 18*rw

' make radio buttons
FOR t=1 to 5
  BUTTON "clr"&t TEXT "⚪️" AT (t-1)*100+260*rw,280*rh SIZE 35*rw,35*rh
NEXT t

' set radio button 1 active
BUTTON "clr1" TEXT "🔘"
clr=1

' make stop button
FILL ALPHA 1
SET BUTTONS FONT SIZE 20*rw
BUTTON "Stop" TEXT "STOP" AT sw*.84,280*rh SIZE 85*rw,35*rw

' main program loop
DO

' test and set radio buttons
for u=1 to 5
  IF bp("clr"&u) THEN
    clr=u
    BUTTON "clr"&u TEXT "🔘"
    gosub offbutt
    ON clr GOTO 10,20,30,40,50
    10 GRAPHICS CLEAR!GOTO skip
    20 GRAPHICS CLEAR 1,1,1!GOTO skip
    30 GRAPHICS CLEAR 0,0,0!GOTO skip
    40 GRAPHICS CLEAR 1,0,0!GOTO skip
    50 GRAPHICS CLEAR 0,0,1
    skip:
  ENDIF
next u

' check stop button
IF bp("Stop") THEN end
SLOWDOWN
UNTIL 0

' set unselected radio buttons off
offbutt:
  for t=1 to 5
    if t<>clr then BUTTON "clr"&t TEXT "⚪️"
  next t
RETURN

DEF bp(a$) = BUTTON_PRESSED(a$)
Attachments
660A07BB-51D8-4258-AC5F-2AADAA2CD3BC.png
660A07BB-51D8-4258-AC5F-2AADAA2CD3BC.png (64.31 KiB) Viewed 1127 times
5E5BAA02-52C2-434D-B23A-0ED56E0AE6F9.png
5E5BAA02-52C2-434D-B23A-0ED56E0AE6F9.png (65.5 KiB) Viewed 1127 times
####### Living the colorful life #######

Post Reply