Page 1 of 1

Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 4:01 am
by Quin
In the manual, the ComboBoxGadget lists #PB_EventType_Change as an event constant, but it only seems to be sent for changing the selection in edit fields, not the actual selection of the combo box, like would actually be useful. :mrgreen:
is there a code somewhere to do this?
Thanks!

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 4:10 am
by BarryG
No, it works. Here's an example from the manual (modified). Change the gadget selection and see.

Code: Select all

UsePNGImageDecoder()
LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png")

If OpenWindow(0, 0, 0, 270, 180, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  ComboBoxGadget(2, 10, 70, 250, 21, #PB_ComboBox_Editable)
  For a = 1 To 5
    AddGadgetItem(2, -1,"ComboBox item " + Str(a))
  Next

  SetGadgetState(2, 2)    ; set (beginning with 0) the third item as active one
  
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget And EventGadget()=2
      Debug "Gadget 2 is now -> "+GetGadgetText(2)
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 4:48 am
by Quin
Sure, but only if you use the editable combo box.
Remove the editable flag, and it no longer works.
I want an event when it does.
Sorry, should've been more specific.

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 7:37 am
by RASHAD
Hi Quin
You are right
See if the next snippet OK with you
#1 :

Code: Select all

UsePNGImageDecoder()
LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png")

If OpenWindow(0, 0, 0, 270, 180, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  ComboBoxGadget(2, 10, 70, 250, 30)
  For a = 0 To 5
    AddGadgetItem(2, -1,"ComboBox item " + Str(a))
  Next

  SetGadgetState(2, 2)    ; set (beginning with 0) the third item as active one
  olditem = GetGadgetState(2)
  Repeat
    Select WaitWindowEvent(1)
      Case 0
        If GetGadgetState(2) <> olditem
          Debug "item changed"
          Debug GetGadgetState(2)
          olditem = GetGadgetState(2)
        EndIf
        
      Case #PB_Event_CloseWindow
        quit = 1
        
    EndSelect
  Until Quit = 1
EndIf
#2 :

Code: Select all

UsePNGImageDecoder()
LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png")

If OpenWindow(0, 0, 0, 270, 180, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  ComboBoxGadget(2, 10, 70, 250, 21)
  For a = 1 To 5
    AddGadgetItem(2, -1,"ComboBox item " + Str(a))
  Next

  SetGadgetState(2, 2)    ; set (beginning with 0) the third item as active one
  oldtxt$ = GetGadgetText(2)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 2
            If GetGadgetText(2) <> oldtxt$
              Debug "Select item changed"
              oldtxt$ = GetGadgetText(2)
            EndIf
        EndSelect
    EndSelect
  Until Quit = 1
EndIf

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 8:48 am
by HeX0R
I might miss something, you mean like this, right?

Code: Select all

If OpenWindow(0, 0, 0, 270, 180, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

	ComboBoxGadget(0, 10, 70, 250, 21)
	For a = 1 To 5
		AddGadgetItem(0, -1, "ComboBox item " + Str(a))
	Next

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_CloseWindow
				Break
			Case #PB_Event_Gadget
				Select EventGadget()
					Case 0
						If EventType() = #PB_EventType_Change
							Debug "Now selected -> " + GetGadgetText(0)
						EndIf
				EndSelect
		EndSelect
	ForEver
EndIf
Works just as expected here?

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 10:27 am
by BarryG
Quin wrote: Wed May 28, 2025 4:48 amSure, but only if you use the editable combo box.
Remove the editable flag, and it no longer works.
Well, it still works here; I only added the editable flag because I thought you wanted it. Here it is working without it (like HeX0R):

Code: Select all

UsePNGImageDecoder()
LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png")

If OpenWindow(0, 0, 0, 270, 180, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  ComboBoxGadget(2, 10, 70, 250, 21)
  For a = 1 To 5
    AddGadgetItem(2, -1,"ComboBox item " + Str(a))
  Next

  SetGadgetState(2, 2)
  
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget And EventGadget()=2
      Debug "Gadget 2 is now -> "+GetGadgetText(2)
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 1:33 pm
by Quin
hmm, you're right.
It doesn't work in my large application, but it does in this small test. I just woke up, so will investigate this in a bit.
Thanks for your help guys!

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 1:37 pm
by Quin
Figured it out...
When you want to bind an event, it helps to actually uncomment the BindEvent call...
:twisted: :oops:

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 1:39 pm
by BarryG
Quin wrote: Wed May 28, 2025 1:33 pmIt doesn't work in my large application, but it does in this small test.
You wouldn't believe how many times I was about to post a bug until I tried it in a small standalone snippet... where it worked. So, I feel your pain. :lol:

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 2:16 pm
by Quin
BarryG wrote: Wed May 28, 2025 1:39 pm
Quin wrote: Wed May 28, 2025 1:33 pmIt doesn't work in my large application, but it does in this small test.
You wouldn't believe how many times I was about to post a bug until I tried it in a small standalone snippet... where it worked. So, I feel your pain. :lol:
Hahaha yup, been there, done that. I normally do the exact same thing, but trying this with my unbounded implementation and seeing it not work, plus the docs saying it only happens when you change the selection of the edit field when I was tired, made me go screw it and post the "bug".
And now we know! :D

Re: Capturing an event when the selected item in a ComboBoxGadget changes

Posted: Wed May 28, 2025 3:19 pm
by HeX0R
We probably need a new magical command, like EnableExplicit.
I vote for:
UnCommentImportantPartsOfCode
:mrgreen: