SpinnerGadget value by keyboard input

Just starting out? Need help? Post your questions and find answers here.
texxsound
User
User
Posts: 11
Joined: Sun Aug 29, 2004 12:04 pm
Location: Austria, EU

SpinnerGadget value by keyboard input

Post by texxsound »

hey there folks!

i just wanted to know how it's possible to enter the value in a spinner object by keyboard AND changing it with the up/down arrows. any easy ways?

thx in advance!
Some do code in sweet delight, some do code in endless night
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi there.

This is from the code archive on http://www.PureArea.net

Code: Select all

; German forum: http://robsite.de/php/pureboard/viewtopic.php?t=1602&highlight=
; Author: GPI
; Date: 04. July 2003

;/ 
;/ Advanced Spin Gadgets 
;/ 
;/ GPI - 04.07.2003 
;/ 
Structure SpinGadgets_ 
  Nr.l 
  max.l 
  min.l 
EndStructure 

NewList SpinGadgets_.SpinGadgets_() 

Procedure FindSpin_(Nr) 
  ok=0 
  ResetList(SpinGadgets_()) 
  Repeat 
    If NextElement(SpinGadgets_()) 
      If SpinGadgets_()\Nr=Nr 
        ok=1 
      EndIf 
    Else 
      ok=-1 
    EndIf 
  Until ok 
  If ok=1 
    ProcedureReturn #True 
  Else 
    ProcedureReturn #False 
  EndIf 
EndProcedure 

Procedure DoSpinGadgetEX(Nr,EventType) 
  If FindSpin_(Nr) 
    a$=GetGadgetText(Nr) 
    a=Val(a$) 
    b=GetGadgetState(Nr) 
    If a>SpinGadgets_()\max : a=SpinGadgets_()\max : do=#True : EndIf 
    If a<SpinGadgets_()\min : a=SpinGadgets_()\min : do=#True : EndIf 
    If b>SpinGadgets_()\max : b=SpinGadgets_()\max : do=#True : EndIf 
    If b<SpinGadgets_()\min : b=SpinGadgets_()\min : do=#True : EndIf 
    If b<>a Or a$<>Str(a) Or do 
      If EventType=#PB_EventType_Change Or EventType=#PB_EventType_Focus Or EventType=#PB_EventType_ReturnKey 
        SetGadgetState(Nr,a) 
        If Str(a)<>a$ 
          SetGadgetText(Nr,Str(a)) 
          SendMessage_(GadgetID(Nr),#em_setsel,0,-1) 
        EndIf 
      Else 
        SetGadgetText(Nr,Str(b)) 
        SetGadgetState(Nr,b) 
      EndIf 
    EndIf 
  EndIf 
EndProcedure 

Procedure DoSpinGadget(Nr) 
  ProcedureReturn DoSpinGadgetEX(Nr,EventType()) 
EndProcedure 

Procedure SetSpinGadget(Nr,state) 
  If FindSpin_(Nr) 
    SetGadgetState(Nr,state) 
    SetGadgetText(Nr,Str(state)) 
    DoSpinGadgetEX(Nr,0) 
  EndIf 
EndProcedure 

Procedure GetSpinGadget(Nr) 
  ProcedureReturn GetGadgetState(Nr) 
EndProcedure 

Procedure CreateSpinGadget(Nr,X,Y,Width,Height,max,min,Start) 
  SpinGadget(Nr,X,Y,Width,Height,0 ,32767) ; no bug! 
  AddElement(SpinGadgets_()) 
  SpinGadgets_()\Nr=Nr 
  SpinGadgets_()\max=max 
  SpinGadgets_()\min=min 
  SetSpinGadget(Nr,Start) 
EndProcedure 

Procedure FreeSpinGadget(Nr) 
  If FindSpin_(Nr) 
    DeleteElement(SpinGadgets_()) 
    FreeGadget(Nr) 
  EndIf 
EndProcedure 

Procedure SetSpinGadgetMax(Nr,max) 
  If FindSpin_(Nr) 
    SpinGadgets_()\max=max 
    DoSpinGadgetEX(Nr,0) 
  EndIf 
EndProcedure 

Procedure SetSpinGadgetMin(Nr,min) 
  If FindSpin_(Nr) 
    SpinGadgets_()\min=min 
    DoSpinGadgetEX(Nr,0) 
  EndIf 
EndProcedure 

Procedure GetSpinGadgetMax(Nr) 
  If FindSpin_(Nr) 
    ProcedureReturn SpinGadgets_()\max 
  EndIf 
EndProcedure 

Procedure GetSpinGadgetMin(Nr) 
  If FindSpin_(Nr) 
    ProcedureReturn SpinGadgets_()\min 
  EndIf 
EndProcedure 

Procedure ResizeSpinGadget(Nr,X,Y,w,h) 
  ResizeGadget(Nr,X,Y,w,h) 
EndProcedure 

;- Little example 

OpenWindow(0,0,200,200,200,#PB_Window_SystemMenu,"Spin-Test") 
CreateGadgetList(WindowID()) 
CreateSpinGadget(10, 10,10, 100,32, 0,50, 40) 

SetSpinGadgetMin(10,0) 
SetSpinGadgetMax(10,20) 
SetSpinGadget(10,15);neuer Wert 

ResizeSpinGadget(10,-1,-1,-1,20) 

Repeat 
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow 
      End 
    Case #PB_Event_Gadget 
      If EventGadgetID()=10 
        DoSpinGadget(10) 
      EndIf 
  EndSelect 
ForEver


If you have the archive, it is in the \Gadgets\SpinGadget folder.

Hope that helps.
@}--`--,-- A rose by any other name ..
texxsound
User
User
Posts: 11
Joined: Sun Aug 29, 2004 12:04 pm
Location: Austria, EU

Post by texxsound »

thx but .. Whoa!
thats really the easiest way?
Some do code in sweet delight, some do code in endless night
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

texxsound wrote:thx but .. Whoa!
thats really the easiest way?
HeHe. Blowed if I know. But thats the code I found and it works .. :wink:

Stick it in an include file (or make a lib) where you can't see all the code and it doesn't bulk out your source file. Then you only have to remember a few procedure names, not many more than the PB gadgetry requires.

Anyhow, success with your project. :)
@}--`--,-- A rose by any other name ..
texxsound
User
User
Posts: 11
Joined: Sun Aug 29, 2004 12:04 pm
Location: Austria, EU

Post by texxsound »

Thx. Looks promising :)
Some do code in sweet delight, some do code in endless night
nessie
User
User
Posts: 60
Joined: Mon Jul 07, 2003 1:19 pm
Location: Glasgow / Scotland
Contact:

Post by nessie »

This was the code I used to set a time gadget. You can input from the keyboard and then change it by the up down buttons. You can modify it for your own use.

Code: Select all

; English forum: http://jconserv.net/purebasic/viewtopic.php?t=7262&highlight=
; Author: nessie
; Date: 18. August 2003

; Input the time using a spin gadget

;**********************************
; Time Spin Gadget
; By Garry Watson aka Nessie
; 17/08/03
; Use as you please
;**********************************

FontID1 = LoadFont(1, "Arial", 12, #PB_Font_Bold)
min = 0
spin = 1
hours = 2
sepa = 3
mins = 4
hr=12:hr$="12"
hWnd = OpenWindow(0,0,0,100,100,#PB_Window_ScreenCentered|#PB_Window_SystemMenu,"TIME")
If hwnd=0 Or CreateGadgetList(hwnd)=0:End:EndIf
If hr=00:hr$="00":EndIf
If state=0:state$="00":EndIf
If hr<10:hr$="0"+Str(hr):EndIf
If hr>9:hr$=Str(hr):EndIf
If state<10:state$="0"+Str(state):EndIf
If state>9:state$=Str(state):EndIf
StringGadget(5, 25, 29, 49, 23,"")
DisableGadget(5,1)
StringGadget(hours, 26, 30, 19, 20, hr$, #PB_String_Numeric | #PB_String_BorderLess)
SetGadgetFont(hours, FontID1)
SendMessage_(GadgetID(hours), #EM_LIMITTEXT, 2, 0)
StringGadget(sepa, 45, 30, 6, 20, ":", #PB_String_BorderLess)
SetGadgetFont(sepa, FontID1)
SendMessage_(GadgetID(sepa), #EM_LIMITTEXT, 1, 0)
SpinGadget(spin, 72, 30, 10, 20, -1, 60)
StringGadget(mins, 51, 30, 20, 20, state$, #PB_String_Numeric | #PB_String_BorderLess)
SetGadgetFont(mins, FontID1)
SendMessage_(GadgetID(mins), #EM_LIMITTEXT, 2, 0)
Repeat
  state=Val(GetGadgetText(4)):SetGadgetState(spin,state)
  string = EventGadgetID ()
  If string=3 : ActivateGadget(4) : EndIf
  EventID.l = WaitWindowEvent()
  If eventid = #PB_EventGadget
  ElseIf eventid = 15
  EndIf
  Select EventID
  Case #PB_EventGadget
    Select EventGadgetID()
    Case spin
      hr=Val(GetGadgetText(2))
      state=GetGadgetState(spin)
      evtp=EventType()
      If state>60:SetGadgetState(spin,59):hr=hr-1:EndIf
      If state>59 And state <70 :hr=hr+1:SetGadgetState(spin,0):state=0:EndIf
      If state<10:SetGadgetText(4,"0"+Str(GetGadgetState(spin))):EndIf
      If state>9:SetGadgetText(4,Str(GetGadgetState(spin))):EndIf
      If hr<0 :hr=23:EndIf
      If hr>23:hr=0:EndIf
      If hr<10:SetGadgetText(2,"0"+Str(hr)):EndIf
      If hr>9:SetGadgetText(2,Str(hr)):EndIf
    EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow
texxsound
User
User
Posts: 11
Joined: Sun Aug 29, 2004 12:04 pm
Location: Austria, EU

Post by texxsound »

Okie Dokie!
Looks shorter and more structured!

thx angain!
Some do code in sweet delight, some do code in endless night
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

spin gadget

Post by PB&J Lover »

Try this post:

viewtopic.php?t=6569&highlight=spin+gadget

Looks like a much better solution.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
JoRo
User
User
Posts: 70
Joined: Sat May 10, 2003 2:03 pm
Location: Essen, Germany
Contact:

Post by JoRo »

to the sample in the german forum:

It is new to me, that PB recognizes the eventtype change.
You have to use two gadgets:
a spingadget, so small that only the arrwows are visible and a string gadget, numerical would be good, where the text is set.

Johannes

Code: Select all

Case #terrace0
              levelarray(filterauswahl,0,11)=GetGadgetState(#terrace0)*10
              SetGadgetText(#terrace0S,Str(GetGadgetState(#terrace0)*10))

Case #terrace0S
              If EventType()=#PB_EventType_Change And Val(GetGadgetText(#terrace0S)) <> GetGadgetState(#terrace0)
                If Val(GetGadgetText(#terrace0S)) > 3760
                  SetGadgetText(#terrace0S,"3760")
                ElseIf Val(GetGadgetText(#terrace0S)) < 0
                  SetGadgetText(#terrace0S,"0")
                EndIf
                SetGadgetState(#terrace0,Val(GetGadgetText(#terrace0S))/10)
terrace0 is the spingadget, terrrace0S ist the stringgadget, min for spin is 0 and max 3760, I have taken a factor of 10 to get the spingadget work better
texxsound
User
User
Posts: 11
Joined: Sun Aug 29, 2004 12:04 pm
Location: Austria, EU

Post by texxsound »

i just read the spec sheet for the new PureBasic 4.00. Is there a better way to do this with pb4?
Some do code in sweet delight, some do code in endless night
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

well, here's how i do it

first i check which gadget caused the event

Code: Select all

Select event_gadget
        Case #g_prefs_compilerun_autosaveinterval
          x_rollspingadget(event_gadget,event_type,0,600)
and all handling is done inside the x_rollspingadget() routine, this works for the buttons as well as the text field, easy does it

Code: Select all

Procedure x_rollspingadget(gadgetnr.l,event.l,min.l,max.l)           ; simplify handling events on spingadgets
  Protected x.l
  ;
  ; in:        gadgetnr.l           - gadget number
  ;            event.l              - event retrieved from waitwindowevent()
  ;            min.l                - minimal value of spinner
  ;            max.l                - maximal value of spinner
  ; retval:
  ; out:
  ;
  ; when processing events, call this routine, and pass the necessary parameters, saves some typing
  ;
  If event = -1 Or event = 1
    SetGadgetText(gadgetnr,Str(GetGadgetState(gadgetnr)))
  Else
    x = Val(GetGadgetText(gadgetnr))
    If x < min Or x > max
      x = x_limit(x,min,max)
      SetGadgetText(gadgetnr,Str(x))
    EndIf
    SetGadgetState(gadgetnr,x)
  EndIf
EndProcedure
now i can use that same routine over and over again for every spinner
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
nessie
User
User
Posts: 60
Joined: Mon Jul 07, 2003 1:19 pm
Location: Glasgow / Scotland
Contact:

Post by nessie »

Here's my time gadget code that works with PB4

Code: Select all

FontID1 = LoadFont(1, "Arial", 12, #PB_Font_Bold) 
min = 0 
#spin = 1 
#hours = 2 
#sepa = 3 
#mins = 4
#time = 5
hr=12:hr$="12" 
hWnd = OpenWindow(0,0,0,100,100,"TIME",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
If hwnd=0 Or CreateGadgetList(hwnd)=0:End:EndIf 
If hr=00:hr$="00":EndIf 
If state=0:state$="00":EndIf 
If hr<10:hr$="0"+Str(hr):EndIf 
If hr>9:hr$=Str(hr):EndIf 
If state<10:state$="0"+Str(state):EndIf 
If state>9:state$=Str(state):EndIf 
StringGadget(5, 25, 29, 49, 23,"") 
DisableGadget(5,1) 
StringGadget(#hours, 26, 30, 19, 20, hr$, #PB_String_Numeric | #PB_String_BorderLess) 
SetGadgetFont(#hours, FontID1) 
SendMessage_(GadgetID(#hours), #EM_LIMITTEXT, 2, 0) 
StringGadget(#sepa, 45, 30, 6, 20, ":", #PB_String_BorderLess) 
SetGadgetFont(#sepa, FontID1) 
SendMessage_(GadgetID(#sepa), #EM_LIMITTEXT, 1, 0) 
SpinGadget(#spin, 72, 30, 10, 20, -1, 60) 
StringGadget(#mins, 51, 30, 20, 20, state$, #PB_String_Numeric | #PB_String_BorderLess) 
SetGadgetFont(#mins, FontID1)
TextGadget(#time,40,60,60,25,"")
SendMessage_(GadgetID(#mins), #EM_LIMITTEXT, 2, 0) 
Repeat 
  string = EventGadget () 
  If string=3 : SetActiveGadget(4) : EndIf 
  EventID.l = WaitWindowEvent() 
  If eventid = #PB_Event_Gadget 
  If EventType() = #PB_EventType_Change
  state=Val(GetGadgetText(4)):SetGadgetState(#spin,state):EndIf 
  EndIf 
  Select EventID 
  Case #PB_Event_Gadget 
    Select EventGadget() 
    Case #spin 
      hr=Val(GetGadgetText(2))
      mins = Val(GetGadgetText(#mins))
      state=GetGadgetState(#spin) 
      If state>60:SetGadgetState(#spin,59):hr=hr-1:EndIf 
      If state>59 And state <70 :hr=hr+1:SetGadgetState(#spin,0):state=0:EndIf 
      If state<10:SetGadgetText(4,"0"+Str(GetGadgetState(#spin))):EndIf 
      If state>9:SetGadgetText(4,Str(GetGadgetState(#spin))):EndIf 
      If hr<0 :hr=23:EndIf 
      If hr>23:hr=0:EndIf 
      If hr<10:SetGadgetText(2,"0"+Str(hr)):EndIf 
      If hr>9:SetGadgetText(2,Str(hr)):EndIf 
      time$ = Str(hr)+":"+GetGadgetText(#mins)
      EndSelect
    EndSelect 
Until EventID = #PB_Event_CloseWindow 
Post Reply