Page 1 of 1

Change color of button

Posted: Mon Apr 24, 2017 8:32 am
by PowerSoft
I wont to change the color of a button under runtime.
When I click on it it should change into red, when I click again back into the original color and so on.

Have no clou how to do that.

some help

Re: Change color of button

Posted: Mon Apr 24, 2017 9:51 am
by IdeasVacuum
You can do it with ButtonImageGadget(). Toggle button images with SetGadgetAttribute().

Re: Change color of button

Posted: Mon Apr 24, 2017 10:41 am
by TI-994A
PowerSoft wrote:I wont to change the color of a button under runtime.
When I click on it it should change into red, when I click again back into the original color and so on.
For a single button:

Code: Select all

Procedure toggleButtonImage()
  Shared btn, redButton, greenButton
  Protected newState
  
  If GetGadgetData(EventGadget()) = redButton
    newState = greenButton
  Else
    newState = redButton
  EndIf
  
  SetGadgetData(btn, newState)
  SetGadgetAttribute(btn, #PB_Button_Image, ImageID(newState))
EndProcedure

redButton = CreateImage(#PB_Any, 200, 50, 32, #Red)
greenButton = CreateImage(#PB_Any, 200, 50, 32, #Green)

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
win = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 400, 300, 
                 "Toggle Button", wFlags)
btn = ButtonImageGadget(#PB_Any, 100, 120, 200, 50, ImageID(redButton))
SetGadgetData(btn, redButton)
BindGadgetEvent(btn, @toggleButtonImage())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
For multiple buttons:

Code: Select all

Procedure toggleButtonImage()
  Shared redButton, greenButton
  Protected newState, btn = EventGadget()
  
  If GetGadgetData(btn) = redButton
    newState = greenButton
  Else
    newState = redButton
  EndIf
  
  SetGadgetData(btn, newState)
  SetGadgetAttribute(btn, #PB_Button_Image, ImageID(newState))
EndProcedure

redButton = CreateImage(#PB_Any, 200, 50, 32, #Red)
greenButton = CreateImage(#PB_Any, 200, 50, 32, #Green)

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
win = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 400, 300, 
                 "Toggle Image Buttons", wFlags)

btn1 = ButtonImageGadget(#PB_Any, 100, 50, 200, 50, ImageID(redButton))
btn2 = ButtonImageGadget(#PB_Any, 100, 120, 200, 50, ImageID(redButton))
btn3 = ButtonImageGadget(#PB_Any, 100, 190, 200, 50, ImageID(redButton))

SetGadgetData(btn1, redButton)
SetGadgetData(btn2, redButton)
SetGadgetData(btn3, redButton)

BindGadgetEvent(btn1, @toggleButtonImage())
BindGadgetEvent(btn2, @toggleButtonImage())
BindGadgetEvent(btn3, @toggleButtonImage())

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Hope it helps. :wink:

Re: Change color of button

Posted: Mon Apr 24, 2017 5:20 pm
by BasicallyPure