[PB5.31] Spin gadget accepting text

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

[PB5.31] Spin gadget accepting text

Post by marcoagpinto »

Hello!

Code: Select all

#Range = 10

If OpenWindow(0, 0, 0, 140, 70, "SpinGadget",#PB_Window_ScreenCentered)
    SpinGadget     (0, 20, 20, 100, 25, -#Range, #Range,#PB_Spin_Numeric)
    SetGadgetState (0, 0) : SetGadgetText(0, "0") 
    Repeat
      Event = WaitWindowEvent()
      
      
    Until Event = #PB_Event_CloseWindow
  EndIf
It allows to write text and characters in the text box.

Shouldn't #PB_Spin_Numeric allow only numbers?
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: [PB5.31] Spin gadget accepting text

Post by Demivec »

marcoagpinto wrote:It allows to write text and characters in the text box.

Shouldn't #PB_Spin_Numeric allow only numbers?
This should be posted in the 'Coding Questions' and not as a bug report.

IMHO it isn't a bug.

According to the documentation:
#PB_Spin_Numeric : The SpinGadget will automatically update the text with value of the state, so SetGadgetText() is not needed.
The use of the flag does not limit what can be typed into the SpinGadget.
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: [PB5.31] Spin gadget accepting text

Post by ostapas »

Had the same problem just 20 minutes ago! :mrgreen: So, in the main window event loop I put this:

Code: Select all

Case #PB_Event_Gadget
  
  If GadgetType(EventGadget()) = #PB_GadgetType_Spin  
    If Not Val(GetGadgetText(EventGadget()))
      SetGadgetState(EventGadget(), 1) ;Whatever default numeric value
    EndIf       
  EndIf
It prevents typing any letters into Spin gadget. Thanks to Fred's anticipation of all possible problems :mrgreen:
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: [PB5.31] Spin gadget accepting text

Post by Tenaja »

ostapas wrote:...;Whatever default numeric value
To be user-friendly, you might consider storing the "current value", and if an invalid entry is made, restore it to the previous value. That way if their edit was accidental, they are relieved of the chore of trying to remember, and then re-entering the value.

Fortunately, the SpinGadget stores it for you...

Code: Select all

			Case #PB_Event_Gadget
				If GadgetType(EventGadget()) = #PB_GadgetType_Spin 
					If Not Val(GetGadgetText(EventGadget()))
						SetGadgetState(0,GetGadgetState(0))    ; reset text to its current numeric value
					EndIf       
				EndIf
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: [PB5.31] Spin gadget accepting text

Post by dibor »

Thanks for the trick, but he do not prevent to put text like 7ut, or 4r ....
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
User avatar
ChrisR
Addict
Addict
Posts: 1154
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: [PB5.31] Spin gadget accepting text

Post by ChrisR »

For Windows, you can add ES_NUMBER style. It allows only digits to be entered into the edit control.
Note that, even with this set, it is still possible to paste non-digits into the edit control.

Code: Select all

SetWindowLongPtr_(GadgetID(#SpinGadget), #GWL_STYLE, GetWindowLongPtr_(GadgetID(#SpinGadget), #GWL_STYLE) | #ES_NUMBER)
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: [PB5.31] Spin gadget accepting text

Post by dibor »

Thank you.
But impossible to put negative numbers :(
Any other solution?
Also would like to know solution for MacOS.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
User avatar
ChrisR
Addict
Addict
Posts: 1154
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: [PB5.31] Spin gadget accepting text

Post by ChrisR »

You can try with something like that:

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Gadgets
  #Spin_1
EndEnumeration

Procedure Open_Window_0(X = 0, Y = 0, Width = 280, Height = 200)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Title", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SpinGadget(#Spin_1, 10, 20, 250, 24, -1000, 1000, #PB_Spin_Numeric)
    SetGadgetState(#Spin_1, 66)
  EndIf
EndProcedure

Open_Window_0()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
      
    Case #WM_CHAR
      If GetActiveGadget() = #Spin_1
        If EventwParam() = 45
          If FindString(GetGadgetText(#Spin_1), "-", 2)
            SetGadgetState(#Spin_1, GetGadgetState(#Spin_1))   
            ;SendMessage_(GadgetID(#Spin_1), #EM_SETSEL, Len(GetGadgetText(#Spin_1)), Len(GetGadgetText(#Spin_1)))
          EndIf
        ElseIf EventwParam() < 48 Or EventwParam() > 58
          SetGadgetState(#Spin_1, GetGadgetState(#Spin_1))
          ;SendMessage_(GadgetID(#Spin_1), #EM_SETSEL, Len(GetGadgetText(#Spin_1)), Len(GetGadgetText(#Spin_1)))
        EndIf
      EndIf
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Spin_1
          SetGadgetState(#Spin_1, GetGadgetState(#Spin_1))
      EndSelect
      
  EndSelect
ForEver
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: [PB5.31] Spin gadget accepting text

Post by dibor »

Thank you Chris.
Still find solution for MacOS.

Best Wishes.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: [PB5.31] Spin gadget accepting text

Post by dibor »

With SetGadgetState every Gadget event do not need to do any manipulations.

Code: Select all

EnableExplicit

Enumeration Window
  #Window_0
EndEnumeration

Enumeration Gadgets
  #Spin_1
EndEnumeration

Procedure Open_Window_0(X = 0, Y = 0, Width = 280, Height = 200)
  If OpenWindow(#Window_0, X, Y, Width, Height, "Title", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SpinGadget(#Spin_1, 10, 20, 250, 24, -1000, 1000, #PB_Spin_Numeric)
    SetGadgetState(#Spin_1, 66)
  EndIf
EndProcedure

Open_Window_0()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Spin_1
            SetGadgetState(#Spin_1, GetGadgetState(#Spin_1))
            Debug GetGadgetText(#Spin_1)
      EndSelect
      
  EndSelect
ForEver
:D
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
User avatar
mk-soft
Always Here
Always Here
Posts: 5405
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [PB5.31] Spin gadget accepting text

Post by mk-soft »

Nice hack :D
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
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: [PB5.31] Spin gadget accepting text

Post by dibor »

In MacOS this trick doesn't works :(
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
Post Reply