Page 1 of 1
SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 6:49 pm
by marcoagpinto
Hello!
I am trying to code a SpinGadget that only accepts numbers between 1 and 999.
I wanted then to limit the number of characters in the gadget to 3 and only allow numbers to be typed.
The problem is that the way I have coded it, it accepts letters and up to 4 characters.
Can someone give a tip?
Thanks!
PS->There is a typo in the PB 5.20 help because in one part it says we don't have to use a SetText but in other example, it uses it.
Kind regards,
>Marco A.G.Pinto
---------------
Code: Select all
; Time
TextGadget(#TEXT_WINDOW_SET_TIME,10+5+2,10+5,100,20+2+1,"Time:")
SpinGadget(#TEXTBOX_WINDOW_SET_TIME,10+5+2+100-20-20-10,10+5-2,100-20-40+5+15-5-5,20, 1, 999,#PB_Spin_Numeric)
x=600
SetGadgetState(#TEXTBOX_WINDOW_SET_TIME, x)
Re: SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 7:29 pm
by davido
You could 'force' both number and length with this sort of construct: Right(Str(Val("12345")),3).
Would this help?
Re: SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 7:58 pm
by TI-994A
marcoagpinto wrote:I am trying to code a SpinGadget that only accepts numbers between 1 and 999.
I wanted then to limit the number of characters in the gadget to 3 and only allow numbers to be typed.
Hi Marco. This example is based on your code snippet. The input is filtered in the SpinGadget's change event to allow only three-digit numerics:
Code: Select all
Enumeration
#MainWindow
#TEXT_WINDOW_SET_TIME
#TEXTBOX_WINDOW_SET_TIME
EndEnumeration
OpenWindow(#MainWindow, 0, 0, 300, 100, "Filtering SpinGadget Entry",
#PB_Window_SystemMenu |
#PB_Window_ScreenCentered)
TextGadget(#TEXT_WINDOW_SET_TIME, 10+5+2, 10+5, 100, 20+2+1, "Time:")
SpinGadget(#TEXTBOX_WINDOW_SET_TIME, 10+5+2+100-20-20-10,
10+5-2, 100-20-40+5+15-5-5, 20, 1, 999, #PB_Spin_Numeric)
x = 600 : SetGadgetState(#TEXTBOX_WINDOW_SET_TIME, x)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case #TEXTBOX_WINDOW_SET_TIME
Select EventType()
Case #PB_EventType_Change
entry$ = GetGadgetText(#TEXTBOX_WINDOW_SET_TIME)
If Len(entry$) < 4
For L = 1 To Len(entry$)
If Asc(Mid(entry$, L, 1)) < 48 Or
Asc(Mid(entry$, L, 1)) > 57
SetGadgetText(#TEXTBOX_WINDOW_SET_TIME, "")
EndIf
Next L
prevEntry$ = entry$
Else
SetGadgetText(#TEXTBOX_WINDOW_SET_TIME, prevEntry$)
EndIf
EndSelect
EndSelect
EndSelect
Until appQuit = 1
A simple implementation, but it should be cross-platform. Hope it works for you.

Re: SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 8:04 pm
by einander
Or try this:
Code: Select all
OpenWindow(0, 100, 100,200,100 ,"", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
Spin1=SpinGadget(#PB_Any,10,10,60,30,1,999,#PB_Spin_Numeric)
SetGadgetState(Spin1,1)
;
Repeat
EV=WaitWindowEvent()
If Ev=#PB_Event_Gadget And EventGadget()=Spin1
A$=GetGadgetText(Spin1)
B=GetGadgetState(Spin1)
If Len(Trim(A$))
For I=48 To 57
If Not FindString(A$,Chr(I))
SetGadgetState(Spin1,B)
Break
EndIf
Next
EndIf
EndIf
Until EV=#PB_Event_CloseWindow
;
Cheers!
Re: SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 9:54 pm
by marcoagpinto
Re: SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 10:25 pm
by einander
Buaaaaaaaa.... why can't there just exist a command to do that?
PB is a set of tools to make programs: wihth PB you can also make thousands of tools.
Commands are just some of these tools.
A programmnig language comprising all the possible commands is an entelechy.
Re: SpinGadget - limit number chrs + only numbers
Posted: Tue Oct 22, 2013 10:31 pm
by IdeasVacuum
It does work, like this:
Code: Select all
If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SpinGadget(1, 20, 20, 60, 25, 1, 999, #PB_Spin_ReadOnly | #PB_Spin_Numeric)
SetGadgetState (1, 100) : SetGadgetText(1, "100") ; set initial value
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
End
Re: SpinGadget - limit number chrs + only numbers
Posted: Wed Oct 23, 2013 2:45 am
by electrochrisso
IdeasVacuum wrote:It does work, like this:
Code: Select all
If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SpinGadget(1, 20, 20, 60, 25, 1, 999, #PB_Spin_ReadOnly | #PB_Spin_Numeric)
SetGadgetState (1, 100) : SetGadgetText(1, "100") ; set initial value
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
End
The problem with this way is if it is not ReadOnly, it is possible to type letters other than numbers, messy.
Perhaps Fred could make it so that if #PB_Spin_Numeric is used only numbers can be typed in.
I think
einander has a good, compact solution, although it is possible to type in an extra three zeros at the front of the number, had a quick go at fixing this, but need a bit more time to find a solution, might even be better to create a custom spin gadget with more options using the canvas.

Re: SpinGadget - limit number chrs + only numbers
Posted: Wed Oct 23, 2013 3:30 am
by IdeasVacuum
The problem with this way is if it is not ReadOnly
........ but this way is ReadOnly and therefore not a problem
I think the behaviour is from the underlying API Control, so Fred isn't necessarily going to change it. A 'hand made' gadget based on the CanvasGadget() would be a good solution (lengthy code though?). Perhaps someone has already made one, worth a search of the Forum.
Re: SpinGadget - limit number chrs + only numbers
Posted: Wed Oct 23, 2013 6:46 am
by electrochrisso
IdeasVacuum wrote:The problem with this way is if it is not ReadOnly
........ but this way is ReadOnly and therefore not a problem
Should have tried your example out, the PB example only needed the #PB_Spin_Numeric value set in the flag, looks like #PB_Spin_Numeric and SetGadgetText(0, Str(GetGadgetState(0))) is the key to this solution.
Anyway I made up this custom spin example before I tried the PB example, still needs a bit of work to get numbers to increment/decrement continuously while holding the buttons down, might come in use to someone.
Code: Select all
OpenWindow(0,100,100,200,100,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SpinNum=StringGadget(#PB_Any,10,15,30,20,"1",#PB_String_Numeric)
SetGadgetAttribute(SpinNum,#PB_String_MaximumLength,3)
SpinUp=ButtonGadget(#PB_Any,40,10,15,15,"U")
SpinDn=ButtonGadget(#PB_Any,40,25,15,15,"D")
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case SpinNum
If Val(GetGadgetText(SpinNum)) < 1
SetGadgetText(SpinNum,"1")
EndIf
Case SpinUp
If Val(GetGadgetText(SpinNum)) <> 999
a=Val(GetGadgetText(SpinNum)) +1
SetGadgetText(SpinNum,Str(a))
EndIf
Case SpinDn
If Val(GetGadgetText(SpinNum)) <> 1
a=Val(GetGadgetText(SpinNum)) -1
SetGadgetText(SpinNum,Str(a))
EndIf
EndSelect
EndSelect
Until Event=#PB_Event_CloseWindow