Enable/disable button with checkboxgadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Enable/disable button with checkboxgadget

Post by Columbo »

I have a CheckBoxGadget and a button. What I would like to be able to do is this,…

If the CheckBox is not checked, the button state is disabled. If the user checks the CheckBox, the button state is immediately enabled. If the CheckBox is enabled and the user unchecks the CheckBox, the button is immediately disabled. To do this, the CheckBox state would have to be constantly monitored for any changes. How can I accomplish this?
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Enable/disable button with checkboxgadget

Post by Demivec »

Perhaps something like this:

Code: Select all

Enumeration window
  #w_Main
EndEnumeration

Enumeration gadget
  #g_CheckBox_chk
  #g_Button_btn
EndEnumeration

Procedure buttonEvents()
  Protected c = Random(200, 60)
  SetWindowColor(#w_Main, RGB(c + 1, c, c - 1))
EndProcedure

Procedure checkBoxEvents()
  Protected state = GetGadgetState(#g_CheckBox_chk)
  Protected output.s = "checkbox state: "
  If state = #PB_Checkbox_Checked  
    output + "Checked"
    SetGadgetText(#g_Button_btn, "Push Me")
    DisableGadget(#g_Button_btn, #False)
  ElseIf state = #PB_Checkbox_Unchecked
    output + "Unchecked"
    SetGadgetText(#g_Button_btn, "Disabled")
    DisableGadget(#g_Button_btn, #True)
  EndIf
  Debug output
EndProcedure

OpenWindow(#w_Main, 0, 0, 300, 100, "Checkbox Test", #PB_Window_SystemMenu)
CheckBoxGadget(#g_CheckBox_chk, 30, 10, 80, 20, "Click Me")
ButtonGadget(#g_Button_btn, 120, 10, 120, 20, "Disabled")

DisableGadget(#g_Button_btn, #True)
BindGadgetEvent(#g_CheckBox_chk, @checkBoxEvents())
BindGadgetEvent(#g_Button_btn, @buttonEvents())

Repeat
  Repeat
    event = WaitWindowEvent()
    If event = #PB_Event_CloseWindow
      quit = #True
    EndIf
  Until event = 0 Or quit = #True
Until quit = #True
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Enable/disable button with checkboxgadget

Post by Columbo »

Thank you very much. Much appreciated!
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Enable/disable button with checkboxgadget

Post by Columbo »

Worked perfectly! Thank you so much.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Enable/disable button with checkboxgadget

Post by Demivec »

Columbo wrote: Thu Jun 10, 2021 5:46 pm Worked perfectly! Thank you so much.
Your welcome.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Enable/disable button with checkboxgadget

Post by BarryG »

This is how I'd do it (if you don't want to bind gadgets):

Code: Select all

OpenWindow(0, 200, 200, 300, 50, "Checkbox Test", #PB_Window_SystemMenu)

CheckBoxGadget(0, 30, 10, 80, 20, "Click Me")

ButtonGadget(1, 120, 10, 120, 20, "Disabled")
DisableGadget(1, #True)

Repeat
  event = WaitWindowEvent()
  If event = #PB_Event_Gadget
    If EventGadget() = 0 ; Checkbox clicked.
      DisableGadget(1, 1 - GetGadgetState(0))
    EndIf
  EndIf
Until event = #PB_Event_CloseWindow
Post Reply