Trouble with Toggle images ?

Just starting out? Need help? Post your questions and find answers here.
millie78526
User
User
Posts: 23
Joined: Thu Apr 18, 2024 9:12 pm

Trouble with Toggle images ?

Post by millie78526 »

TIA :
I'm having Trouble with Toggle images ?
https://vmars.us/ShowMe/WorkingButton- ... -\bmp.bmp
https://vmars.us/ShowMe/StartButton-Gr ... 2-bmp.bmp
See code below:
Problem is that code doesn't show either image .

Code: Select all

; www.purebasic.com
; Author: Andre Beer  (PureBasic-Team  -  www.purebasic.com) (updated for PB4.00 by blbltheworm)
; Date: 4. May 2003
; OS: Windows
; Demo: Yes
; TOGGLE-2-Images.pb -Millie78526
Image1 = LoadImage(0,"StartButton-Green-159x42-bmp.bmp") 
Image2 = LoadImage(1,"WorkingButton-Red-159x42-bmp.bmp") 

If OpenWindow(0, 10, 100, 320,180, "ButtonImageGadget & SetGadgetState", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 
;    ButtonImageGadget(0,10,10,291,155,ImageID)
    ButtonImageGadget(0,10,10,159,42,ImageID)
    Repeat 
Debug "Repeat"
        EventID.l = WaitWindowEvent() 
        If EventID = #PB_Event_Gadget
          gad.l = EventGadget()
          If gad = 0
             If ImageID = Image1
               ImageID = Image2
             Else
               ImageID = Image1
             EndIf
            SetGadgetState(0,ImageID)
  ;          Delay(2000)
          EndIf
        EndIf 
        If EventID = #PB_Event_CloseWindow 
            Quit = 1 
        EndIf 
        
    Until Quit = 1 
EndIf 
End 
Last edited by millie78526 on Tue May 21, 2024 8:03 pm, edited 1 time in total.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Trouble with Toggle images ?

Post by Michael Vogel »

Code: Select all

Image1 = LoadImage(0,"StartButton-Green-159x42-bmp.bmp")
Image2 = LoadImage(1,"WorkingButton-Red-bmp.bmp")
;ShowLibraryViewer("image",1)

If OpenWindow(0, 10, 100, 320,180, "ButtonImageGadget & SetGadgetState", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
	;    ButtonImageGadget(0,10,10,291,155,ImageID)
	ButtonImageGadget(0,10,10,159,42,#Null)
	Repeat
		Select WaitWindowEvent()
		Case #PB_Event_Gadget
			If EventGadget()=0
				If ImageID = Image1
					ImageID = Image2
				Else
					ImageID = Image1
				EndIf
				Debug "SET "+ImageId
				SetGadgetAttribute(0,#PB_Button_Image,ImageID)
				;          Delay(2000)
			EndIf
		Case #PB_Event_CloseWindow
			Quit = 1
		EndSelect
	Until Quit = 1
EndIf
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Trouble with Toggle images ?

Post by Michael Vogel »

The second code - which works here - uses SetGadgetAttribute instead of SetGadgetState.
millie78526
User
User
Posts: 23
Joined: Thu Apr 18, 2024 9:12 pm

Re: Trouble with Toggle images ?

Post by millie78526 »

Thanks Michael Vogel ,
I couldn't get that working ,
so I switched things around a bit .
Now switch occurs at Button Click
and I added FreeGadget(0) .

Code: Select all

Image1 = LoadImage(0, "StartButton-Green-159x42-bmp.bmp")
Image2 = LoadImage(1, "WorkingButton-Red-159x42-bmp.bmp")
ImageID = Image1 ; Initialize ImageID with Image1

If OpenWindow(0, 10, 100, 320, 180, "ButtonImageGadget & SetGadgetState", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
    ButtonImageGadget(0, 10, 10, 159, 42, ImageID)
    ButtonImageGadget(0, 10, 10, 159, 42, ImageID)
    Repeat
        EventID = WaitWindowEvent()
        Select EventID
            Case #PB_Event_Gadget
                Select EventGadget()
                    Case 0
                        If ImageID = Image1
                            ImageID = Image2
                            Debug "ImageID switched to Image2"
                        Else
                            ImageID = Image1
                            Debug "ImageID switched to Image1"
                        EndIf
                        FreeGadget(0)
                        ButtonImageGadget(0, 10, 10, 159, 42, ImageID)
                EndSelect
                
            Case #PB_Event_CloseWindow
                Quit = 1
        EndSelect
    Until Quit = 1
EndIf
End
Post Reply