SpinGadget increment

Just starting out? Need help? Post your questions and find answers here.
troy
User
User
Posts: 51
Joined: Sat May 31, 2003 2:59 pm

SpinGadget increment

Post by troy »

Hi,

Please take a look at the tiny example below. There is a problem with the increment - if you press the left button for a while. You can see that the increment is changed to 5 (40-45-50...) instead of 1 (41-42-43...). Does anyone know how to get rid of that behaviour? I need to have +1 increment always, no matter how long a user presses the SpinGadget.

Edit: ...or even force a user to press spingadget each time he wants to increase the value, e.g. disable autoincrement at all.

Code: Select all

If OpenWindow(0,0,0,140,70,"SpinGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
SpinGadget (0,20,20,100,25,0,1000) 
SetGadgetState (0,5) : SetGadgetText(0,"5") ; set initial value 
Repeat 
Event = WaitWindowEvent() 

Select Event 
Case #PB_Event_Gadget 
Select EventGadget() 
Case 0 
n.l = GetGadgetState(0)
SetGadgetText(0,Str(n)) 
Debug n
WindowEvent()
EndSelect 
EndSelect 
Until Event = #PB_Event_CloseWindow 
EndIf
--
troy
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

The incrementing of values like that is standard windows behaviour for that kind of gadget. What you can do to get around this though is to handle all the value incrementing and decrementing yourself.

Also i noticed that you didn't have a 'CreateGadgetList()' command in your code. All windows need this before any gadget creation! ;)

Anyway, here's an updated piece of code to handle the values yourself:

Code: Select all

#SPINNER_GADGET = 0
Global SpinnerValue.l = 5

Procedure AdjustValue(Gadget.l, Direction.l, CurrentValue.l)
	Select Gadget
		Case #SPINNER_GADGET
			Select Direction
				Case 1
					SpinnerValue = CurrentValue + 1
					If SpinnerValue > 1000
						SpinnerValue = 1000
					EndIf
				Case -1
					SpinnerValue = CurrentValue - 1
					If SpinnerValue < 0
						SpinnerValue = 0
					EndIf
			EndSelect
			SetGadgetText(#SPINNER_GADGET, Str(SpinnerValue))
	EndSelect
EndProcedure

If OpenWindow(0,0,0,140,70,"SpinGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	If CreateGadgetList(WindowID(0))
		SpinGadget(#SPINNER_GADGET,20,20,100,25,0,1000, #PB_Spin_ReadOnly)
		SetGadgetText(#SPINNER_GADGET, Str(SpinnerValue))
		Repeat
			Event = WaitWindowEvent()
			Select Event
				Case #PB_Event_Gadget
					Select EventGadget()
						Case #SPINNER_GADGET
							AdjustValue(0, EventType(), SpinnerValue)
					EndSelect
			EndSelect
		Until Event = #PB_Event_CloseWindow
	EndIf
EndIf
End
The spinners value is kept in the var; 'SpinnerValue'.
Last edited by Kale on Sat May 20, 2006 7:03 pm, edited 1 time in total.
--Kale

Image
troy
User
User
Posts: 51
Joined: Sat May 31, 2003 2:59 pm

Post by troy »

Kale wrote: Also i noticed that you didn't have a 'CreateGadgetList()' command in your code. All windows need this before any gadget creation! ;)
Actually, I did. It's located in very first "IF" along with OpenWindow.
And your example has it doubled... :twisted:

Thanks, Kale for the help! Your example works perfectly, it's what I'm looking for.
--
troy
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

troy wrote:Actually, I did. It's located in very first "IF" along with OpenWindow.
And your example has it doubled... :twisted:
I must of missed it, i've corrected my example. :wink:
--Kale

Image
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Hi,

I know this code is old but i need it for the increment to be + 10

so i did this

Code: Select all


#SPINNER_GADGET = 0
Global SpinnerValue = 10

Procedure AdjustValue(Gadget.l, Direction.l, CurrentValue.l)
  Select Gadget
    Case #SPINNER_GADGET
      Select Direction
        Case 1
          SpinnerValue = CurrentValue + 10
          If SpinnerValue > 1000
            SpinnerValue = 1000
          EndIf
        Case -1
          SpinnerValue = CurrentValue - 10
          If SpinnerValue < 10
            SpinnerValue = 10
          EndIf
      EndSelect
      Debug Direction 
      Debug "Spinner value: " + Str(SpinnerValue)
      SetGadgetText(Gadget, Str(SpinnerValue))
  EndSelect
EndProcedure

If OpenWindow(0,0,0,140,70,"SpinGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If CreateGadgetList(WindowID(0))
    SpinGadget(#SPINNER_GADGET,20,20,100,25,0,1000, #PB_Spin_ReadOnly)
    SetGadgetText(#SPINNER_GADGET, Str(SpinnerValue))
    Repeat
      Event = WaitWindowEvent()
      Select Event
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #SPINNER_GADGET
              AdjustValue(0, EventType(), SpinnerValue)
          EndSelect
      EndSelect
    Until Event = #PB_Event_CloseWindow
  EndIf
EndIf
End 
Run the code with debugger on and you will see that there is a problem with the EventType() .

Here in this short snippet it shows -1 when i click the down arrow of the spingadget.

If i click on the up arrow it will give me a large number, not 1 .

I took this snippet to usage in one of my programs and it gives me also
not 1 or -1 results.

If you also notice the first click is ALWAYS the large number
after that it will be -1 if you click it 2nd time.

I am talking of course on the Direction.l var in the procedure.

Someone knows why EventType() gives wrong numbers?

thank you.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Working as expected here except for the first click thing. After that it will work as intended. I think the number of 256 is equal to the #EN_SETFOCUS notification for the edit control of the spin gadget. So you should only update the value after recieving the focus and exclude this message:

Code: Select all

If EventType() = 1 Or EventType() = -1
	AdjustValue(0,EventType(),SpinnerValue)
EndIf
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Following code contains API for Windows only....

Code: Select all

#UDM_SETPOS32 = #WM_USER + 113 
#UDM_GETPOS32 = #WM_USER + 114 

Dim uda.UDACCEL(0)
;- Lock SpinGadget at increments of 1
uda(0)\nSec = 0
uda(0)\nInc = 1

If OpenWindow(0,0,0,140,70,"SpinGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  SpinGadget (0,20,20,100,25,0,1000) 
  SetGadgetState (0,5) : SetGadgetText(0,"5") ; set initial value 
  ;- Get the handle to the UpDown control 
  hSpin = FindWindowEx_(WindowID(0), GadgetID(0), "msctls_updown32", 0) 
  ;- SEt the increment value
  SendMessage_(hSpin, #UDM_SETACCEL, 1, @uda(0))
  Repeat 
    event = WaitWindowEvent() 
    
    Select event 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 0 
            ;- Get the position of the UpDown control
            n.l = SendMessage_(hSpin, #UDM_GETPOS32, 0, 0)
            SetGadgetText(0, Str(n)) 
            Debug n 
        EndSelect 
    EndSelect 
  Until event = #PB_Event_CloseWindow 
EndIf 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Thanks sparkie!

This works great :)
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Fluid Byte wrote:Working as expected here except for the first click thing. After that it will work as intended. I think the number of 256 is equal to the #EN_SETFOCUS notification for the edit control of the spin gadget. So you should only update the value after recieving the focus and exclude this message:

Code: Select all

If EventType() = 1 Or EventType() = -1
	AdjustValue(0,EventType(),SpinnerValue)
EndIf
Well as i mentioned if i implement this code in my huge project, ALL the clicks are not -1 or 1 so i need to skip this EventType() check..

Hope it is not a bug in PB.
Post Reply