Page 1 of 1

Solved: How to update an Image Gadget correctly

Posted: Sat Nov 22, 2025 4:47 pm
by SMaag
what is the correct way to update the Image of an Image Gadget?

There is no offical way how to update the Image if the Image changed after Color Transformation functions.

After greyscaling the Image of an Image Gadget it is not updated automatically.

Use Gadget State and set the #Image once again do not work.

The way I found is a resize of the Image Gadget. But in my opinion this can't be the right way!

Re: How to update an Image Gadget correctly

Posted: Sat Nov 22, 2025 5:47 pm
by mk-soft
PB-Help (F1)
- SetGadgetState(): Change the current Image of the gadget. A valid ImageID can be easily obtained with the ImageID() function. If the ImageID is 0, then the image is removed from the gadget.

Re: How to update an Image Gadget correctly

Posted: Sat Nov 22, 2025 6:12 pm
by SMaag
I tried SetGadgetState(). Does not work.
And if I set first Image=0 and then again the Image I get a white screen.
I have to resize always the ImageGadget to actualize with the modified Image.

PB6.21

I guess I will make a DemoCode to show the Problem.

But what is the offical way to update a modified Image on the Gadget?

Re: How to update an Image Gadget correctly

Posted: Sat Nov 22, 2025 7:33 pm
by mk-soft
Works fine here with macOS, Windows, Linux

Code: Select all

;-TOP

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
  ResizeGadget(1, 10, dy - 35, 120, 25)
  ResizeGadget(2, 140, dy - 35, 120, 25)
  ResizeGadget(3, 270, dy - 35, 120, 25)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("&File")
    MenuItem(99, "E&xit")
    
    ; StatusBar
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    
    ; Images
    image1.s = #PB_Compiler_Home + "examples/sources/Data/AlphaChannel.bmp"
    LoadImage(1, image1)
    image2.s = #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp"
    LoadImage(2, image2)
    
    ; Gadgets
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    ImageGadget(0, 10, 10, 400, 240, ImageID(2), #PB_Image_Border)
    ButtonGadget(1, 10, dy - 35, 120, 25, "Image 1")
    ButtonGadget(2, 140, dy - 35, 120, 25, "Image 2")
    ButtonGadget(3, 270, dy - 35, 120, 25, "No Image")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case 0
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case 99
              PostEvent(#PB_Event_CloseWindow, 0, 0)
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              SetGadgetState(0, ImageID(1))
            Case 2
              SetGadgetState(0, ImageID(2))
            Case 3
              SetGadgetState(0, 0)
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Solved: How to update an Image Gadget correctly

Posted: Sun Nov 23, 2025 10:20 pm
by SMaag
oohh!!
As I saw your code, I realized my fault.
Now it works!
Thank you very much!

I forgot the ImageID() Function at SetGadgetState()

Code: Select all

SetGadgetState(#GG_Image, ImageID(actImgNo))
; SetGadgetState(#GG_Image, actImgNo) ; That was the fault

Re: Solved: How to update an Image Gadget correctly

Posted: Sun Nov 23, 2025 10:35 pm
by infratec
And again ...

many people who wants to help invest time and energy only because there is no working or not working example which shows the problem.

With your missing example code the problem where solved in 5 minutes.

You need 10 minutes to write such a code.
10 'helpers' needs 10 x 10 minutes to write examples. 9 x 10 minutes where wasted.

Re: Solved: How to update an Image Gadget correctly

Posted: Mon Nov 24, 2025 9:41 am
by SMaag
And again ...

many people who wants to help invest time and energy only because there is no working or not working example which shows the problem.

With your missing example code the problem where solved in 5 minutes.

You need 10 minutes to write such a code.
10 'helpers' needs 10 x 10 minutes to write examples. 9 x 10 minutes where wasted.
I know that! But I did not ask for a working code!
I did not ask for solving my problem - I just described it!
I just asked what is the standard way to update an ImageGadget because in the documentation there is no comment for that issue.

So if anyone knows. The simple response is: SetGadgetState() is the standard command to update an image!
Furthermore in my first response I wrote: "I will make an example which shows my problem!"

And once again. Thanks for the help!

Re: Solved: How to update an Image Gadget correctly

Posted: Mon Nov 24, 2025 10:09 am
by BarryG
SMaag wrote: Mon Nov 24, 2025 9:41 amI just asked what is the standard way to update an ImageGadget because in the documentation there is no comment for that issue.
Yes there is. mk-soft linked to it above.