Optiongadget and EventTypes
Optiongadget and EventTypes
Hi,
the OptionGadget doesn't seem to react on any EventTypes (and the help also says so). So, when I post a #PB_EventType_LeftClick or #PB_EventType_Change nothing happens.
As for me, I usually BindGadgetEvent() most of the OptionGadgets and don't have to worry to enable or disable other gadgets. But if I want my program to change the state I surely can use SetGadgetState, but the binded function is not called. Is this intentional? I think triggering LeftClick or Change would be a logical way to handle this.
the OptionGadget doesn't seem to react on any EventTypes (and the help also says so). So, when I post a #PB_EventType_LeftClick or #PB_EventType_Change nothing happens.
As for me, I usually BindGadgetEvent() most of the OptionGadgets and don't have to worry to enable or disable other gadgets. But if I want my program to change the state I surely can use SetGadgetState, but the binded function is not called. Is this intentional? I think triggering LeftClick or Change would be a logical way to handle this.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Optiongadget and EventTypes
This didn't help? -> viewtopic.php?p=582924
Last edited by BarryG on Fri Feb 24, 2023 10:37 am, edited 1 time in total.
-
- Addict
- Posts: 4789
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Optiongadget and EventTypes
Sorry, I don't understand what exactly you want to do with Option Gadgets and the EventType() function.jacdelad wrote: Fri Feb 24, 2023 1:27 am Hi,
the OptionGadget doesn't seem to react on any EventTypes (and the help also says so). So, when I post a #PB_EventType_LeftClick or #PB_EventType_Change nothing happens.
As for me, I usually BindGadgetEvent() most of the OptionGadgets and don't have to worry to enable or disable other gadgets. But if I want my program to change the state I surely can use SetGadgetState, but the binded function is not called. Is this intentional? I think triggering LeftClick or Change would be a logical way to handle this.
Can you post a short code snippet that demonstrates your intended usage?
Re: Optiongadget and EventTypes
From too much time
Update 2
Update 2
Code: Select all
;-TOP
Enumeration Windows
#Main
EndEnumeration
Enumeration Menus
#MainMenu
EndEnumeration
Enumeration MenuItems
#MainMenuExitApplication
EndEnumeration
Enumeration Gadgets
#MainOptionContainer_1
#MainOption_11
#MainOption_12
#MainOptionContainer_2
#MainOption_21
#MainOption_22
#MainFunction_1
#MainFunction_2
#MainFunction_3
#MainFunction_4
EndEnumeration
Enumeration Status
#MainStatusBar
EndEnumeration
; ----
Procedure doEventOptionGroup_1()
Static last_gadget = -1
Protected gadget = EventGadget()
Select EventType()
Case #PB_EventType_LeftClick
If gadget <> last_gadget
last_gadget = gadget
Select gadget
Case #MainOption_11
Debug "Do Option 1.1"
;
Case #MainOption_12
Debug "Do Option 1.2"
;
EndSelect
EndIf
EndSelect
EndProcedure
Procedure doEventOptionGroup_2()
Static last_gadget = -1
Protected gadget = EventGadget()
Select EventType()
Case #PB_EventType_LeftClick
If gadget <> last_gadget
last_gadget = gadget
Select gadget
Case #MainOption_21
Debug "Do Option 2.1"
;
Case #MainOption_22
Debug "Do Option 2.2"
;
EndSelect
EndIf
EndSelect
EndProcedure
Procedure SetDefaultOptionGadget(Window, Gadget)
SetGadgetState(Gadget, #True)
PostEvent(#PB_Event_Gadget, Window, Gadget, #PB_EventType_LeftClick)
EndProcedure
; ----
Procedure UpdateWindow()
Protected dx, dy
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
; Resize Gadgets
ResizeGadget(#MainFunction_1, 10, dy - 35, 110, 30)
ResizeGadget(#MainFunction_2, 130, dy - 35, 110, 30)
ResizeGadget(#MainFunction_3, 250, dy - 35, 110, 30)
ResizeGadget(#MainFunction_4, 370, dy - 35, 110, 30)
EndProcedure
Procedure Main()
Protected dx, dy
#WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
; MenuBar
CreateMenu(0, WindowID(0))
MenuTitle("File")
MenuItem(#MainMenuExitApplication, "E&xit")
; StatusBar
CreateStatusBar(#MainStatusBar, WindowID(#Main))
AddStatusBarField(#PB_Ignore)
; Gadgets
dx = WindowWidth(#Main)
dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
ContainerGadget(#MainOptionContainer_1, 5, 5, 220, 75, #PB_Container_Single)
OptionGadget(#MainOption_11, 10, 10, 200, 25, "Option 1.1")
OptionGadget(#MainOption_12, 10, 40, 200, 25, "Option 1.2")
CloseGadgetList()
ContainerGadget(#MainOptionContainer_2, 5, 85, 220, 75, #PB_Container_Single)
OptionGadget(#MainOption_21, 10, 10, 200, 25, "Option 2.1")
OptionGadget(#MainOption_22, 10, 40, 200, 25, "Option 2.2")
CloseGadgetList()
ButtonGadget(#MainFunction_1, 10, dy - 35, 110, 30, "Function 1")
ButtonGadget(#MainFunction_2, 130, dy - 35, 110, 30, "Function 2")
ButtonGadget(#MainFunction_3, 250, dy - 35, 110, 30, "Function 3")
ButtonGadget(#MainFunction_4, 370, dy - 35, 110, 30, "Function 4")
; Bind Events
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
BindGadgetEvent(#MainOption_11, @doEventOptionGroup_1())
BindGadgetEvent(#MainOption_12, @doEventOptionGroup_1())
BindGadgetEvent(#MainOption_21, @doEventOptionGroup_2())
BindGadgetEvent(#MainOption_22, @doEventOptionGroup_2())
; Main Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Select EventWindow()
Case #main
Break
EndSelect
Case #PB_Event_Menu
Select EventMenu()
Case #MainMenuExitApplication
PostEvent(#PB_Event_CloseWindow, #Main, #Null)
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #MainFunction_1
SetDefaultOptionGadget(#Main, #MainOption_11)
Case #MainFunction_2
SetDefaultOptionGadget(#Main, #MainOption_12)
Case #MainFunction_3
SetDefaultOptionGadget(#Main, #MainOption_21)
Case #MainFunction_4
SetDefaultOptionGadget(#Main, #MainOption_22)
EndSelect
EndSelect
ForEver
EndIf
EndProcedure : Main()
Last edited by mk-soft on Fri Feb 24, 2023 4:16 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Optiongadget and EventTypes
Thanks for the example
P.D: You do a mistake in the second event gadget numbers

P.D: You do a mistake in the second event gadget numbers

Re: Optiongadget and EventTypes
Update
Yes,
with Enumeration is always better
Yes,
with Enumeration is always better

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Addict
- Posts: 4789
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Optiongadget and EventTypes
mk-soft wrote: Fri Feb 24, 2023 11:37 amCode: Select all
Procedure doEventOptionGroup_1() Static last_gadget = -1 Protected gadget = EventGadget() Select EventType() Case #PB_EventType_LeftClick If gadget <> last_gadget last_gadget = gadget Select gadget Case #MainOption_11 Debug "Do Option 1.1" ; Case #MainOption_12 Debug "Do Option 1.2" ; EndSelect EndIf EndSelect EndProcedure Procedure doEventOptionGroup_2() Static last_gadget = -1 Protected gadget = EventGadget() Select EventType() Case #PB_EventType_LeftClick If gadget <> last_gadget last_gadget = gadget Select gadget Case #MainOption_21 Debug "Do Option 2.1" ; Case #MainOption_22 Debug "Do Option 2.2" ; EndSelect EndIf EndSelect EndProcedure
EventType() is not needed at all here. You can just do it like this:
Code: Select all
Procedure doEventOptionGroup_1()
Static last_gadget = -1
Protected gadget = EventGadget()
If gadget <> last_gadget
last_gadget = gadget
Select gadget
Case #MainOption_11
Debug "Do Option 1.1"
;
Case #MainOption_12
Debug "Do Option 1.2"
;
EndSelect
EndIf
EndProcedure
Procedure doEventOptionGroup_2()
Static last_gadget = -1
Protected gadget = EventGadget()
If gadget <> last_gadget
last_gadget = gadget
Select gadget
Case #MainOption_21
Debug "Do Option 2.1"
;
Case #MainOption_22
Debug "Do Option 2.2"
;
EndSelect
EndIf
EndProcedure
Re: Optiongadget and EventTypes
I always process the EventType, because I also sometimes create and send my own even types to gadgets.
The event type #PB_EventType_LeftClick (0) is also always available.
The event type #PB_EventType_LeftClick (0) is also always available.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Addict
- Posts: 4789
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Optiongadget and EventTypes
Yes, maybe. But I am talking about the code that was posted here. And using functions in code posted here that are not necessary at all can easily confuse less experienced readers. Not a good idea.mk-soft wrote: Fri Feb 24, 2023 3:32 pm I always process the EventType, because I also sometimes create and send my own even types to gadgets.
see also viewtopic.php?p=471641#p471641
Re: Optiongadget and EventTypes
@Little John
Right, not needed here. But it tends to be forgotten when it is needed.
@jacdelad
A call to SetGadgetState is not a user action. Therefore, no event is triggered. However, you can trigger it yourself if necessary. See SetDefaultOptionGadget ...
Link Update 2: viewtopic.php?p=596414#p596414
Right, not needed here. But it tends to be forgotten when it is needed.
@jacdelad
A call to SetGadgetState is not a user action. Therefore, no event is triggered. However, you can trigger it yourself if necessary. See SetDefaultOptionGadget ...
Link Update 2: viewtopic.php?p=596414#p596414
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Optiongadget and EventTypes
@BarryG: Argh, I don't remember that I have posted this before. Obviously this haunts my regularly...
To explain my problem:
This is basically the code to react on the OptionGadgets. Now I want to manually set Option0 active and trigger these events too. Calling the function doesn't work, because EventGadget() is not set/valid. Naturally I would add something like
or
to let Option 0 receive an event...
...but OptionGadgets don't handle events. EventType() is always 0 within ReactOnOption().
So my question is (and obviously was), why does this not work or is this not intended to work?
To explain my problem:
Code: Select all
OpenWindow(0,0,0,400,300,"Window",#PB_Window_ScreenCentered)
OptionGadget(0,0,0,400,20,"Option1")
OptionGadget(1,0,0,400,20,"Option1")
Procedure ReactOnOption()
Select EventGadget()
Case 0
;Do Stuff when Option 0 is active
Case 1
;Do Stuff when Option 0 is active
EndSelect
EndProcedure
BindGadgetEvent(0,@ReactOnOption())
BindGadgetEvent(1,@ReactOnOption())
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Code: Select all
PostEvent(#PB_Event_Gadget,0,0,#PB_EventType_LeftClick)
Code: Select all
PostEvent(#PB_Event_Gadget,0,0,#PB_EventType_Change)
...but OptionGadgets don't handle events. EventType() is always 0 within ReactOnOption().
So my question is (and obviously was), why does this not work or is this not intended to work?
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Optiongadget and EventTypes
You need to check the state
Code: Select all
OpenWindow(0,0,0,400,300,"Window",#PB_Window_ScreenCentered)
OptionGadget(0,0,0,400,20,"Option1")
OptionGadget(1,0,20,400,20,"Option2")
Procedure ReactOnOption()
Select EventGadget()
Case 0
If GetGadgetState(0)
Debug "option1 set"
EndIf
Case 1
If GetGadgetState(1)
Debug "option2 set"
EndIf
;Do Stuff when Option 0 is active
EndSelect
EndProcedure
BindGadgetEvent(0,@ReactOnOption())
BindGadgetEvent(1,@ReactOnOption())
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
-
- Addict
- Posts: 4789
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Optiongadget and EventTypes
When PostEvent() is used properly, everything works fine. EventType() is not supported by OptionGadget(), because it's not needed at all in this context.
See freak's explanation on this topic.
//edit 2023-02-26:
Moved SetGadgetState() from the callback procedure to the event loop, since it is only necessary there.
See freak's explanation on this topic.
//edit 2023-02-26:
Moved SetGadgetState() from the callback procedure to the event loop, since it is only necessary there.
Code: Select all
; PB 6.00 LTS
EnableExplicit
#WinMain = 0
; Gadgets
Enumeration
#Option0
#Option1
#Button0
#Button1
EndEnumeration
Procedure OptionEvent()
Select EventGadget()
Case 0
Debug "Option 0 chosen"
Case 1
Debug "Option 1 chosen"
EndSelect
EndProcedure
Define event.i
OpenWindow(#WinMain, 0, 0, 300, 150, "Window", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OptionGadget(#Option0, 20, 10, 80, 20, "Option0")
OptionGadget(#Option1, 20, 40, 80, 20, "Option1")
ButtonGadget(#Button0, 20, 100, 80, 30, "Button0")
ButtonGadget(#Button1, 200, 100, 80, 30, "Button1")
BindGadgetEvent(#Option0, @OptionEvent())
BindGadgetEvent(#Option1, @OptionEvent())
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case #Button0
SetGadgetState(#Option0, 1)
PostEvent(#PB_Event_Gadget, #WinMain, #Option0)
Case #Button1
SetGadgetState(#Option1, 1)
PostEvent(#PB_Event_Gadget, #WinMain, #Option1)
EndSelect
EndSelect
Until event = #PB_Event_CloseWindow
Last edited by Little John on Sun Feb 26, 2023 3:38 pm, edited 1 time in total.
Re: Optiongadget and EventTypes
Ah, that's what I was looking for. I never got the idea of simply not using the EventType. Thanks a lot!
...and also I was not alone with this problem.
...and also I was not alone with this problem.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD