track bar gadget not working with #pb_eventType_change

Just starting out? Need help? Post your questions and find answers here.
nsstudios
Enthusiast
Enthusiast
Posts: 274
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

track bar gadget not working with #pb_eventType_change

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

Re: track bar gadget not working with #pb_eventType_change

Post 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.
PeDe
Enthusiast
Enthusiast
Posts: 119
Joined: Sun Nov 26, 2017 3:13 pm
Location: Vienna
Contact:

Re: track bar gadget not working with #pb_eventType_change

Post by PeDe »

Code: Select all

If e=#PB_Event_Gadget And EventType()=#PB_EventType_LeftClick
Peter
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: track bar gadget not working with #pb_eventType_change

Post 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
Egypt my love
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: track bar gadget not working with #pb_eventType_change

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
nsstudios
Enthusiast
Enthusiast
Posts: 274
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: track bar gadget not working with #pb_eventType_change

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

Re: track bar gadget not working with #pb_eventType_change

Post 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
Egypt my love
nsstudios
Enthusiast
Enthusiast
Posts: 274
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: track bar gadget not working with #pb_eventType_change

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: track bar gadget not working with #pb_eventType_change

Post 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
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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: track bar gadget not working with #pb_eventType_change

Post 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
BERESHEIT
nsstudios
Enthusiast
Enthusiast
Posts: 274
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: track bar gadget not working with #pb_eventType_change

Post by nsstudios »

Thanks so much for the help everyone.
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: track bar gadget not working with #pb_eventType_change

Post by BarryG »

I posted this in the Wishlist section for you (and I) -> https://www.purebasic.fr/english/viewto ... =3&t=74922
Jeff8888
User
User
Posts: 38
Joined: Fri Jan 31, 2020 6:48 pm

Re: track bar gadget not working with #pb_eventType_change

Post 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

nsstudios
Enthusiast
Enthusiast
Posts: 274
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: track bar gadget not working with #pb_eventType_change

Post by nsstudios »

Thanks.
Post Reply