SpinGadget - limit number chrs + only numbers

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

SpinGadget - limit number chrs + only numbers

Post 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)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: SpinGadget - limit number chrs + only numbers

Post by davido »

You could 'force' both number and length with this sort of construct: Right(Str(Val("12345")),3).
Would this help?
DE AA EB
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: SpinGadget - limit number chrs + only numbers

Post 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. :)
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
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: SpinGadget - limit number chrs + only numbers

Post 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!
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: SpinGadget - limit number chrs + only numbers

Post by marcoagpinto »

Buaaaaaaaa.... why can't there just exist a command to do that?

If I selected that it is a numerical spin gadget, why doesn't it automatically only accept numbers?

:( :( :( :( :( :( :( :( :( :( :(
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: SpinGadget - limit number chrs + only numbers

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: SpinGadget - limit number chrs + only numbers

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: SpinGadget - limit number chrs + only numbers

Post 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. :idea:
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. :)
PureBasic! Purely the best 8)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: SpinGadget - limit number chrs + only numbers

Post by IdeasVacuum »

The problem with this way is if it is not ReadOnly
........ but this way is ReadOnly and therefore not a problem :mrgreen:

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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: SpinGadget - limit number chrs + only numbers

Post 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 :mrgreen:
:lol:

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. :wink:

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
PureBasic! Purely the best 8)
Post Reply