Page 1 of 1
track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 12:52 pm
by nsstudios
Hi all,
I added the track bar gadget to my program, and noticed that it doesn't appear to send #pb_eventType_change, so I'm curious what's the best way to go about checking for changes?
I could always store the previous result of GetGadgetStatus and compare it in the loop, but there must be a better way?
Code: Select all
EnableExplicit
OpenWindow(0, #PB_IGNORE, #PB_IGNORE, #PB_IGNORE, #PB_IGNORE, "test", #PB_Window_ScreenCentered|#PB_Window_Maximize)
TrackBarGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_TrackBar_Vertical)
;SpinGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_Spin_Numeric)
Repeat
Define e=WaitWindowEvent()
If e=#PB_Event_Gadget And EventType()=#PB_EventType_Change
SetWindowTitle(0, ""+GetGadgetState(0))
EndIf
Until e=#PB_Event_CloseWindow
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 1:04 pm
by BarryG
nsstudios wrote:I could always store the previous result of GetGadgetStatus and compare it in the loop
That's the only way at the moment. I don't use TrackBars much, so I'm surprised that #PB_EventType_Change can't be used with them. You should post this in the Wishlist section of the forum.
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 1:12 pm
by PeDe
Code: Select all
If e=#PB_Event_Gadget And EventType()=#PB_EventType_LeftClick
Peter
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 1:34 pm
by RASHAD
Hi
There is no such EventType() for the TrackBarGadget()
Read the manual please
Code: Select all
OpenWindow(0, 0,0,800,600, "test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TrackBarGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_TrackBar_Vertical)
;SpinGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_Spin_Numeric)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 0
SetWindowTitle(0, "Thump at : "+GetGadgetState(0))
EndSelect
EndSelect
Until Quit = 1
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 1:42 pm
by TI-994A
nsstudios wrote:...added the track bar gadget to my program, and noticed that it doesn't appear to send #pb_eventType_change, so I'm curious what's the best way to go about checking for changes? ...
Try something like this:
Code: Select all
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 600, 400, "TrackBar 1 = 0", wFlags)
TrackBarGadget(0, 0, 0, 50, 400, 0, 100, #PB_TrackBar_Vertical)
TrackBarGadget(1, 60, 0, 50, 400, 0, 100, #PB_TrackBar_Vertical)
TextGadget(2, 120, 10, 460, 50, "TrackBar 2 = 0", #PB_Text_Right)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = #True
Case #PB_Event_Gadget
Select EventGadget()
Case 0
SetWindowTitle(0, "TrackBar 1 = " + GetGadgetState(0))
Case 1
SetGadgetText(2, "TrackBar 2 = " + GetGadgetState(1))
EndSelect
EndSelect
Until appQuit
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 2:43 pm
by nsstudios
Wow, thank you all for the help.
I totally forgot to try and see what I get when catching any event at all.
It's interesting that it fires left click instead of change,and even more so that it fires it twice for every change.
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 2:50 pm
by RASHAD
You are checking if Change just happened
There is a hack using CanvasGadget() by Danilo
Search the forum for it if you like more options
No need for LeftClick twice
Try the next
Code: Select all
OpenWindow(0, 0,0,800,600, "test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TrackBarGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_TrackBar_Vertical)
;SpinGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_Spin_Numeric)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 0
pos = DesktopMouseY()
SetWindowTitle(0, "Thump at : "+GetGadgetState(0))
Delay(10)
If DesktopMouseY() <> pos
Debug "Position changed from :"+ pos +" to "+DesktopMouseY()
EndIf
EndSelect
EndSelect
Until Quit = 1
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 3:35 pm
by nsstudios
Perhaps I was unclear.
When I change the track bar gadget once (one press of up/down arrow), it fires the left click event twice, which isn't a problem for the situation I need it for in my program, but could maybe be problematic in other cases.
Code: Select all
EnableExplicit
OpenWindow(0, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore, "test", #PB_Window_ScreenCentered|#PB_Window_Maximize)
TrackBarGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), 0, 100, #PB_TrackBar_Vertical)
Define count=0
Repeat
Define e=WaitWindowEvent()
If e=#PB_Event_Gadget And EventGadget()=0 And EventType()=#PB_EventType_LeftClick
count+1
SetWindowTitle(0, "("+count+")"+GetGadgetState(0))
EndIf
Until e=#PB_Event_CloseWindow
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 5:03 pm
by mk-soft
Save last position in gadget data...
Code: Select all
Procedure DoEventTrackBar()
Protected gadget = EventGadget()
Protected position = GetGadgetState(gadget)
Select EventType()
Case #PB_EventType_LeftClick
If position <> GetGadgetData(gadget)
SetGadgetData(gadget, position)
PostEvent(#PB_Event_Gadget, GetActiveWindow(), gadget, #PB_EventType_Change, position)
EndIf
EndSelect
EndProcedure
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 600, 400, "TrackBar 1 = 0", wFlags)
TrackBarGadget(0, 0, 0, 50, 400, 0, 100, #PB_TrackBar_Vertical)
TrackBarGadget(1, 60, 0, 50, 400, 0, 100, #PB_TrackBar_Vertical)
TextGadget(2, 120, 10, 460, 50, "TrackBar 2 = 0", #PB_Text_Right)
BindGadgetEvent(0, @DoEventTrackBar())
BindGadgetEvent(1, @DoEventTrackBar())
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = #True
Case #PB_Event_Gadget
Select EventGadget()
Case 0
Select EventType()
Case #PB_EventType_Change
SetWindowTitle(0, "TrackBar 1 = " + GetGadgetState(0))
EndSelect
Case 1
Select EventType()
Case #PB_EventType_Change
SetGadgetText(2, "TrackBar 2 = " + EventData())
EndSelect
EndSelect
EndSelect
Until appQuit
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 5:43 pm
by netmaestro
Mon coup de pied au chat:
Code: Select all
Macro HIWORD(val)
val>>16
EndMacro
Macro LOWORD(val)
val&$FFFF
EndMacro
#PB_Event_TrackbarChanged = #PB_Event_FirstCustomValue
Procedure TrackbarHandler0()
Static oldstate.i=0
If EventType() = #PB_EventType_LeftClick
newstate=GetGadgetState(0)
If Not newstate=oldstate
statevalues.l = oldstate<<16|newstate
PostEvent(#PB_Event_TrackbarChanged, #PB_Ignore, #PB_Ignore, #PB_Ignore, statevalues)
oldstate=newstate
EndIf
EndIf
EndProcedure
Procedure TrackChangeHandler0()
statevalues = EventData()
oldstate = HIWORD(statevalues)
newstate = LOWORD(statevalues)
SetGadgetText(1, "Trackbar Old: "+oldstate)
SetGadgetText(2, "Trackbar New: "+newstate)
EndProcedure
OpenWindow(0,0,0,320,240,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadget(1,10,10,120,20,"Trackbar Old: 0")
TextGadget(2,140,10,120,20,"Trackbar New: 0")
TrackBarGadget(0, 0,215,320,20,0,1000)
BindGadgetEvent(0, @TrackbarHandler0())
BindEvent(#PB_Event_TrackbarChanged, @TrackChangeHandler0())
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Re: track bar gadget not working with #pb_eventType_change
Posted: Sat Mar 21, 2020 8:10 pm
by nsstudios
Thanks so much for the help everyone.
Re: track bar gadget not working with #pb_eventType_change
Posted: Sun Mar 22, 2020 1:31 am
by BarryG
I posted this in the Wishlist section for you (and I) ->
https://www.purebasic.fr/english/viewto ... =3&t=74922
Re: track bar gadget not working with #pb_eventType_change
Posted: Sun Mar 22, 2020 7:31 pm
by Jeff8888
Here is code to do what you want I believe
Code: Select all
;Demo program to display value of TrackBar
;I used help file on TrackBarGadget for lines 4 to 13
;Note for a scroll bar if you want to display its value while scolling
;you need To Bindevent because there are no events while scolling
If OpenWindow(0, 0, 0, 320, 200, "TrackBarGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget (3, 10, 20, 250, 20,"TrackBar Standard", #PB_Text_Center)
TrackBarGadget(0, 10, 40, 250, 20, 0, 10000)
SetGadgetState(0, 5000)
TextGadget (4, 10, 100, 250, 20, "TrackBar Ticks", #PB_Text_Center)
TrackBarGadget(1, 10, 120, 250, 20, 0, 5, #PB_TrackBar_Ticks)
SetGadgetState(1, 3)
TextGadget (5, 90, 180, 200, 20, "TrackBar Vertical", #PB_Text_Right)
TrackBarGadget(2, 270, 10, 20, 170, 0, 10000, #PB_TrackBar_Vertical)
SetGadgetState(2, 8000)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_CloseWindow
End
EndIf
If event = #PB_Event_Gadget
Select EventGadget()
Case 1 ;add more for other gadgets as needed
NewValue=GetGadgetState(1)
If NewValue<>OldValue
Debug NewValue
NewValueFlag=#True
OldValue=NewValue
Else
NewValueFlag=#False
EndIf
EndSelect
EndIf
ForEver
EndIf
End
Re: track bar gadget not working with #pb_eventType_change
Posted: Mon Mar 23, 2020 7:47 am
by nsstudios
Thanks.