Capturing an event when the selected item in a ComboBoxGadget changes

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Capturing an event when the selected item in a ComboBoxGadget changes

Post 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!
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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
Egypt my love
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

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

Post 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?
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post 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!
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post by Quin »

Figured it out...
When you want to bind an event, it helps to actually uncomment the BindEvent call...
:twisted: :oops:
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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:
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

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

Post by HeX0R »

We probably need a new magical command, like EnableExplicit.
I vote for:
UnCommentImportantPartsOfCode
:mrgreen:
Post Reply