stringgadget #PB_String_Float general numerical input

Just starting out? Need help? Post your questions and find answers here.
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

stringgadget #PB_String_Float general numerical input

Post by Al_the_dutch »

Hi All,

For numerical input of all kinds of numbers (positive, negative, integers, floats) I use Stringgadget with #PB_String_Numeric but it is not good enough as it does only accept 0-9 as characters. So it is not possible to make a "1" turn into "-1" or "1.1". From "1.1" to "1.2" is no problem.

With a #PB_String_Float my idea is it should only accept 0-9, but also a - (minus) and . (or the local decimal seperator) as characters.
But unfortunately, #PB_String_Float does not exist.

Any suggestions/work arounds?
Thx!
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: stringgadget #PB_String_Float general numerical input

Post by Keya »

one option is to make your own 'mask/filter' for whatever characters you like, which is fairly easy as it turns out, for example i needed to accept hex chars 0-9/a-f/A-F ... http://www.purebasic.fr/english/viewtop ... 96#p491296
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: stringgadget #PB_String_Float general numerical input

Post by Al_the_dutch »

Thanx!
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: stringgadget #PB_String_Float general numerical input

Post by Al_the_dutch »

It will going to be something like this:

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)
    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 And Not bNegativeIntegers
                new$ + Chr(*p\c)
            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'
            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")
   
    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
    
    Repeat
        event = WaitWindowEvent()
        If event = #PB_Event_CloseWindow 
           Debug "Inputs were " + GetGadgetText(0) + " & " + GetGadgetText(1) + " & " +
                 GetGadgetText(2) + " & " + GetGadgetText(3) + " & " + GetGadgetText(4)
           Debug "Values were " + ValD(GetGadgetText(0)) + " & " + ValD(GetGadgetText(1)) + " & " +
                 ValD(GetGadgetText(2)) + " & " + GetGadgetText(3) + " & " + ValD(GetGadgetText(4))
           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
                 checkFloatInput(EventGadget(), #True, #True)
              EndIf
              
            EndIf
        EndIf
    ForEver
 EndIf
 
 End ; EndItAll
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: stringgadget #PB_String_Float general numerical input

Post by Keya »

seems to work fine on my XP32, i like it also how for example the minus sign can only be at the start and there can only be one decimal place
Al_the_dutch
User
User
Posts: 70
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: stringgadget #PB_String_Float general numerical input

Post by Al_the_dutch »

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
Post Reply