Two examples for simple number/float (non-scientific notation) input:
Code: Select all
EnableExplicit
Procedure checkFloatInput(gadget)
Protected start, count, pointcount, new$
SendMessage_(GadgetID(gadget), #EM_GETSEL, @start, 0)
Protected txt$ = GetGadgetText(gadget)
Protected *p.Character = @txt$
While *p\c ; <> 0
If *p\c = '.'
pointcount+1
If pointcount < 2
new$ + Chr(*p\c)
Else
If start>count : start-1 : EndIf
EndIf
ElseIf count = 0 And *p\c = '-'
new$ + Chr('-')
ElseIf *p\c >= '0' And *p\c <= '9'
new$ + Chr(*p\c)
Else
start - 1
EndIf
*p + SizeOf(Character)
count + 1
Wend
SetGadgetText(gadget, new$)
SendMessage_(GadgetID(gadget), #EM_SETSEL, start, start)
EndProcedure
Define event
If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8, 10, 306, 20, "")
StringGadget(1, 8, 35, 306, 20, "1234567")
Repeat
event = WaitWindowEvent()
If event = #PB_Event_CloseWindow : End
ElseIf event = #PB_Event_Gadget
If EventGadget()=0 Or EventGadget()=1
If EventType() = #PB_EventType_Change
checkFloatInput(EventGadget())
EndIf
EndIf
EndIf
ForEver
EndIf
; ts-soft: Habe Danilos Codebeispiel mal um Minus-Werte erweitert
The following was for german forum, so it uses comma ',' instead dot '.' => for example "567,88" (can be changed within the regular expression)
Code: Select all
;
; by Danilo
;
; http://www.purebasic.fr/german/viewtopic.php?f=16&t=26388&start=7
;
; Nummern-Eingabe Beispiele: 123 567,88 -12 -4,5
;
OpenWindow(0, #PB_Ignore, #PB_Ignore, 250, 80, "")
StringGadget(0, 10, 10, 200, 20, "")
CreateRegularExpression(0,"^\-{0,1}\d*$|^\-{0,1}\d+\,\d{0,2}$|^$") ; ^ = Anfang des Strings
; $ = Ende des Strings
; \d = Dezimalzahl 0-9
; + = 1 oder mehr Vorkommen
; * = 0 oder mehr Vorkommen
; | = alternative Moeglichkeit (Or in PureBasic)
; \, = Komma
; \-{0,1} = Minus{0 oder 1 mal}
; \d{0,2} = Dezimalzahl 0-9 { 0 bis 2 mal }
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
If EventGadget() = 0 And EventType() = #PB_EventType_Change
txt$ = GetGadgetText(0)
If MatchRegularExpression(0,txt$)=0 ; wenn kein Treffer, dann wieder vorherigen text setzen
SendMessage_(GadgetID(0),#EM_GETSEL,0,@endpos) : endpos - 1 ; cursor position holen
SetGadgetText(0,old$) ; alten text wieder setzen
SendMessage_(GadgetID(0),#EM_SETSEL,endpos,endpos) ; cursor position wieder setzen
Else
old$ = txt$
EndIf
EndIf
EndSelect
ForEver
Translated English version, uses dot '.' for float numbers:
Code: Select all
;
; by Danilo
;
; Original german version: http://www.purebasic.fr/german/viewtopic.php?f=16&t=26388&start=7
;
; Number-Input, English version
; Examples: 123 567.88 -12 -4.5
;
OpenWindow(0, #PB_Ignore, #PB_Ignore, 250, 80, "")
StringGadget(0, 10, 10, 200, 20, "")
CreateRegularExpression(0,"^\-{0,1}\d*$|^\-{0,1}\d+\.\d{0,2}$|^$") ; ^ = Begin of string
; $ = End of string
; \d = Decimal number 0-9
; + = 1 or more occurences
; * = 0 or more occurences
; | = alternative ('Or' in PureBasic)
; \. = Dot
; \-{0,1} = Minus{0 or 1 times}
; \d{0,2} = Decimal number 0-9 { 0 to 2 times }
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
If EventGadget() = 0 And EventType() = #PB_EventType_Change
txt$ = GetGadgetText(0)
If MatchRegularExpression(0,txt$)=0 ; If regular expression does not match: set old text
SendMessage_(GadgetID(0),#EM_GETSEL,0,@endpos) : endpos - 1 ; get cursor position
SetGadgetText(0,old$) ; set old text
SendMessage_(GadgetID(0),#EM_SETSEL,endpos,endpos) ; set cursor position
Else
old$ = txt$
EndIf
EndIf
EndSelect
ForEver
All examples use WinAPI messages #EM_GETSEL & #EM_SETSEL to save and restore the cursor position. Otherwise it would be platform-independent.
I already requested the possibility of handling the cursor and text selection numerous times, because of such example codes, but the team resists to add it.
Maybe it is too powerful, or just not useful for freak.
