ComboBox and detecting index change
ComboBox and detecting index change
I have a single combobox that i need to detect just a change in index for (ie: the user selects a new entry in the combobox).
I tried with EventType() and #PB_EventType_Change but that doesn't seem to work.
Any ideas?
Tim
I tried with EventType() and #PB_EventType_Change but that doesn't seem to work.
Any ideas?
Tim
Code: Select all
If OpenWindow(0,0,0,270,140,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"ComboBoxGadget") And CreateGadgetList(WindowID(0))
ComboBoxGadget(0,10,40,250,100)
For a=1 To 5
AddGadgetItem(0,-1,"ComboBox item "+Str(a))
Next
SetGadgetState(0,2)
Active_Item.l = GetGadgetState(0)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadgetID()
Case 0
If Active_Item <> GetGadgetState(0)
Active_Item = GetGadgetState(0)
Debug "Changed"
;Do whatever you like...
EndIf
EndSelect
EndSelect
ForEver
EndIf
End
{Home}.:|:.{Dialog Design0R}.:|:.{Codes}.:|:.{History Viewer Online}.:|:.{Send a Beer}
Thank you very much, HeX0R.
You may never come around to read this reply, but just the same, I wanted to let you know that your so simple code is just perfect.
I've modified it for PB 4.xx and added a textBox so we can see right away the results of your efforts:
You may never come around to read this reply, but just the same, I wanted to let you know that your so simple code is just perfect.
I've modified it for PB 4.xx and added a textBox so we can see right away the results of your efforts:
Code: Select all
If 0 = OpenWindow(0,0,0,270,140,"ComboBoxGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
End
EndIf
;ComboBoxGadget(0,10,10,250,100) ;; before 4.40
ComboBoxGadget(0,10,10,250,22) ;; for 4.40 and above
For a=1 To 5
AddGadgetItem(0,-1,"ComboBox item "+Str(a))
Next
SetGadgetState(0,2)
TextGadget(1,10,40,230,20,"99 - selected ComboBox item")
active_Item.l = GetGadgetState(0)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow : Break
Case #PB_Event_Gadget : Select EventGadget()
Case 0
If active_Item <> GetGadgetState(0)
active_Item = GetGadgetState(0)
SetGadgetText(1,Str(active_Item) + ": " + GetGadgetText(0))
; Debug "Changed = " + Str(Active_Item)
; Do whatever you like...
EndIf
EndSelect
EndSelect
ForEver
Last edited by Blue on Fri Apr 30, 2010 8:32 am, edited 1 time in total.
PB Forums : Proof positive that 2 heads (or more...) are better than one 
-
gnozal
- PureBasic Expert

- Posts: 4229
- Joined: Sat Apr 26, 2003 8:27 am
- Location: Strasbourg / France
- Contact:
Other possibility [Windows only (#CBN_SELCHANGE)] :
Code: Select all
Enumeration
#Window_0
EndEnumeration
Enumeration
#ComboBox_0
#Text_1
EndEnumeration
Procedure OpenWindow_Window_0()
If OpenWindow(#Window_0, 450, 200, 265, 112, "#CBN_SELCHANGE", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
If CreateGadgetList(WindowID(#Window_0))
ComboBoxGadget(#ComboBox_0, 15, 15, 240, 100)
AddGadgetItem(#ComboBox_0, -1, "Item 1")
AddGadgetItem(#ComboBox_0, -1, "Item 2")
AddGadgetItem(#ComboBox_0, -1, "Item 3")
SetGadgetState(#ComboBox_0, 0)
TextGadget(#Text_1, 15, 55, 240, 20, "")
SetGadgetText(#Text_1, GetGadgetText(#ComboBox_0))
EndIf
EndIf
EndProcedure
OpenWindow_Window_0()
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
EventGadget = EventGadget()
EventType = EventType()
If EventGadget = #ComboBox_0 And EventType = #CBN_SELCHANGE
SetGadgetText(#Text_1, GetGadgetText(#ComboBox_0))
EndIf
Case #PB_Event_CloseWindow
EventWindow = EventWindow()
If EventWindow = #Window_0
CloseWindow(#Window_0)
Break
EndIf
EndSelect
ForEverFor free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
- DoubleDutch
- Addict

- Posts: 3220
- Joined: Thu Aug 07, 2003 7:01 pm
- Location: United Kingdom
- Contact:
Code: Select all
Enumeration
#Window_0
EndEnumeration
Enumeration
#ComboBox_0
#Text_1
EndEnumeration
Procedure OpenWindow_Window_0()
If OpenWindow(#Window_0, 450, 200, 265, 112, "#PB_EventType_RightClick", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
If CreateGadgetList(WindowID(#Window_0))
ComboBoxGadget(#ComboBox_0, 15, 15, 240, 100)
AddGadgetItem(#ComboBox_0, -1, "Item 1")
AddGadgetItem(#ComboBox_0, -1, "Item 2")
AddGadgetItem(#ComboBox_0, -1, "Item 3")
SetGadgetState(#ComboBox_0, 0)
TextGadget(#Text_1, 15, 55, 240, 20, "")
SetGadgetText(#Text_1, GetGadgetText(#ComboBox_0))
EndIf
EndIf
EndProcedure
OpenWindow_Window_0()
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
EventGadget = EventGadget()
EventType = EventType()
If EventGadget = #ComboBox_0 And EventType = #PB_EventType_RightClick
SetGadgetText(#Text_1, GetGadgetText(#ComboBox_0))
EndIf
Case #PB_Event_CloseWindow
EventWindow = EventWindow()
If EventWindow = #Window_0
CloseWindow(#Window_0)
Break
EndIf
EndSelect
ForEverhttps://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
https://reportcomplete.com <- School end of term reports system
1=1
Code: Select all
Debug #CBN_SELCHANGE
Debug #PB_EventType_RightClickMerci Gnozal...gnozal wrote:Other possibility [Windows only (#CBN_SELCHANGE)] :Code: Select all
[...] If EventGadget = #ComboBox_0 And EventType = #CBN_SELCHANGE SetGadgetText(#Text_1, GetGadgetText(#ComboBox_0)) EndIf [...]
Your well-structures solution works really well, and is also even simpler (plus lots more elegant!) than HeXOR's,
if the programmer knows (or remembers)...
(a) that the constant #CBN_SELCHANGE exists somewhere in the Windows Universe
(b) the context in which to make proper use of that mysterious constant.
Personally, I prefer code that relies strictly on documented features, functions and constants available in PB's Help file.
That's what I love so much about heXOR's code. It uses a standard feature in a clever way that was not apparent to the PB newbie that I am.
Knowing your demonstrated ability in Windows programming, your solution is probably more in conformity with the good and recommended practices of Windows programming. But it relies, with its use of #CBN_SELCHANGE, on insider knowledge.
A PB newbie like me wouldn't even have known where to begin looking for this constant and what to do with it. Plus, 5 weeks or 5 months from now, it still won't be documented in PB's material ! Whereas GetGadgetState() is a permanent fixture of PB.
---------------------
2 days later:
PS: That being said, I ended up using your solution because it makes for neater code
PB Forums : Proof positive that 2 heads (or more...) are better than one 
- Andre
- PureBasic Team

- Posts: 2148
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
I found no answer to my question here in the forum yet.
But this topic is a good start for code examples, and to explain my wanted behaviour:
I would like to know not every index change in the combobox.
Instead I want to know the choosen* combobox item, if its different than before.
* So the "new item - event" should be fired, not already if the user is browsing through the combobox items. Instead the event should be fired, if user finish the browsing by
a) pressing left mouse button (if browsing listed combobox items with mouse)
b) pressing Return key (if browsing the combobox list with the cursor keys)
Hope you understand my problem/wish.
Any working code example showing this behaviour would be fine.
Code without API is prefered, because it should be cross-plattform (Windows & MacOS).
Many thanks in advance!
But this topic is a good start for code examples, and to explain my wanted behaviour:
I would like to know not every index change in the combobox.
Instead I want to know the choosen* combobox item, if its different than before.
* So the "new item - event" should be fired, not already if the user is browsing through the combobox items. Instead the event should be fired, if user finish the browsing by
a) pressing left mouse button (if browsing listed combobox items with mouse)
b) pressing Return key (if browsing the combobox list with the cursor keys)
Hope you understand my problem/wish.
Any working code example showing this behaviour would be fine.
Code without API is prefered, because it should be cross-plattform (Windows & MacOS).
Many thanks in advance!
I'm not sure if the following suits your needs? I don't know of a way of achieving what you're after without using API I'm afraid.
Code: Select all
Procedure callback(hWnd, uMsg, wParam, lParam)
Static selItem
Result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_COMMAND
Select wParam>>16&$ffff
Case #CBN_DROPDOWN
If lParam = GadgetID(0)
selItem = GetGadgetState(0)
EndIf
Case #CBN_CLOSEUP
If lParam = GadgetID(0) And SelItem <> GetGadgetState(0)
Debug "Selected item " + Str(GetGadgetState(0))
EndIf
Case #CBN_SELENDOK
If lParam = GadgetID(0)
If SendMessage_(lParam, #CB_GETDROPPEDSTATE, 0, 0) = 0
selItem = GetGadgetState(0)
Debug "Selected item " + Str(selItem)
EndIf
EndIf
EndSelect
EndSelect
ProcedureReturn Result
EndProcedure
If OpenWindow(0, 0, 0, 270, 140, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
ComboBoxGadget(0, 10, 10, 250, 100)
For a = 0 To 9 : AddGadgetItem(0, -1,"ComboBox item " + Str(a)) : Next a
SetWindowCallback(@callback())
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
I may look like a mule, but I'm not a complete ass.
- Andre
- PureBasic Team

- Posts: 2148
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Thank you very much, srod!srod wrote:I'm not sure if the following suits your needs? I don't know of a way of achieving what you're after without using API I'm afraid.
After a short text it looks like it works how I wanted it to work. Must include it my main program to have a final test
The API isn't the problem itself. It's just a problem for cross-compiling then - but the same example adapted to the API of MacOS would solve this problem (using CompilerIf with OS constants) . Does anyone know how to get the same behaviour on MacOS?
