Change color of button

Just starting out? Need help? Post your questions and find answers here.
PowerSoft
User
User
Posts: 65
Joined: Sun Aug 16, 2015 2:54 pm

Change color of button

Post 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
OS X 10.10.5 PB 5.31(x64)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Change color of button

Post by IdeasVacuum »

You can do it with ButtonImageGadget(). Toggle button images with SetGadgetAttribute().
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Change color of button

Post 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:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Change color of button

Post by BasicallyPure »

BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Post Reply