About the minus, I did not see any problem. About the decimals, is this OK for you?
Code: Select all
EnableExplicit
; Basically http://www.purebasic.fr/german/viewtopic.php?f=3&t=25210&start=4 by ts-soft and Danilo
Procedure checkFloatInput(gadget, bNegativeOK.b = #True, bNegativeIntegers.b = #False, nDecimals.u = 9999)
Protected start, count, pointcount, new$, nPosPoint.a
SendMessage_(GadgetID(gadget), #EM_GETSEL, @start, 0)
Protected txt$ = GetGadgetText(gadget)
Protected *p.Character = @txt$
While *p\c
If *p\c = '.' And count > 0
pointcount+1
If pointcount < 2 And Not bNegativeIntegers
new$ + Chr(*p\c)
nPosPoint = count
Else
If start>count : start-1 : EndIf
EndIf
ElseIf count = 0 And *p\c = '-' And bNegativeOK
new$ + Chr('-')
ElseIf *p\c >= '0' And *p\c <= '9' And Count - nPosPoint <= nDecimals
new$ + Chr(*p\c)
Else
start - 1
EndIf
*p + SizeOf(Character)
count + 1
Wend
If bNegativeIntegers
If Len(new$) = 0 Or new$ = "-" Or ValD(new$) >= 0
new$ = "-1"
EndIf
EndIf
SetGadgetText(gadget, new$)
SendMessage_(GadgetID(gadget), #EM_SETSEL, start, start)
EndProcedure ; checkFloatInput
Define event
If OpenWindow(0, 0, 0, 422, 205, "StringGadget several types input", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(10, 8, 10, 90, 20, "Positive integers")
TextGadget(11, 8, 35, 90, 20, "Float +/-")
TextGadget(12, 8, 60, 90, 20, "Float +")
TextGadget(13, 8, 85, 90, 20, "All")
TextGadget(14, 8, 110, 90, 20, "Negative integers")
TextGadget(15, 8, 135, 90, 30, "Float with max 3 decimals")
StringGadget(0, 118, 10, 296, 20, "31415", #PB_String_Numeric) ; Positive integers
StringGadget(1, 118, 35, 296, 20, "-3.1415") ; Float +/-
StringGadget(2, 118, 60, 296, 20, "3.1415") ; Float only +
StringGadget(3, 118, 85, 296, 20, "3.Abcd1415") ; all strings
StringGadget(4, 118, 110, 296, 20, "-31415") ; Negative integers
StringGadget(5, 118, 135, 296, 20, "3.142") ; Float with max 3 decimals
Repeat
event = WaitWindowEvent()
If event = #PB_Event_CloseWindow
Debug "Inputs were " + GetGadgetText(0) + " & " + GetGadgetText(1) + " & " +
GetGadgetText(2) + " & " + GetGadgetText(3) + " & " + GetGadgetText(4) +
" & " + GetGadgetText(5)
Debug "Values were " + ValD(GetGadgetText(0)) + " & " + ValD(GetGadgetText(1)) + " & " +
ValD(GetGadgetText(2)) + " & " + GetGadgetText(3) + " & " + ValD(GetGadgetText(4)) +
" & " + ValD(GetGadgetText(5))
End ; EndItAll
ElseIf event = #PB_Event_Gadget
If EventType() = #PB_EventType_Change
If EventGadget() = 1 ; List of floating point +/- gadgets
checkFloatInput(EventGadget(), #True)
ElseIf EventGadget() = 2 ; ; List of floating point but positive gadgets
checkFloatInput(EventGadget(), #False)
ElseIf EventGadget() = 4 ; ; List of negative integers - gadgets
checkFloatInput(EventGadget(), #True, #True)
ElseIf EventGadget() = 5; ; List of floats with 3 decimals - gadgets
checkFloatInput(EventGadget(), #True, #False, 3)
EndIf
EndIf
EndIf
ForEver
EndIf
End ; EndItAll