Page 1 of 1

Decimal number entry

Posted: Thu Jan 09, 2025 5:33 pm
by jak64
Good morning,
The goal of this little routine is to be able to enter a decimal number by only accepting the digits from 0 to 9 and possibly only one decimal point.
If an invalid character is entered, I delete it and update the display in "StringGadget".

The problem is that the cursor returns to the start of the input area whereas I would like it to be placed at the end of the input area.

Can you help me?

Thanks to you

Code: Select all

OpenWindow(0,100,100, 300,200, "TEST")
StringGadget(1, 10, 10, 100, 20, "123.45")
to_leave = #False
Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      to_leave=#True
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Select EventType()
            Case #PB_EventType_Change
              Amount.s = GetGadgetText(1)
              chain_length = Len(Amount)
              decimal_point = 0
              For i = 1 To chain_length
                character.s = Mid(Amount ,i, 1)
                If Not FindString("0123456789.", character)
                  Amount = Left(Amount, chain_length - 1)
                  SetGadgetText(1, Amount)
                EndIf
                If FindString(".", character)
                  decimal_point + 1
                  If decimal_point > 1
                    Amount = Left(Amount, chain_length - 1)
                    SetGadgetText(1, Amount)
                  EndIf
                EndIf
              Next i     
          EndSelect
      EndSelect
  EndSelect
Until to_leave

Re: Decimal number entry

Posted: Thu Jan 09, 2025 5:37 pm
by Quin
How's this?

Code: Select all

OpenWindow(0,100,100, 300,200, "TEST")
StringGadget(1, 10, 10, 100, 20, "123.45")
to_leave = #False
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
to_leave=#True
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Select EventType()
Case #PB_EventType_Change
Amount.s = GetGadgetText(1)
chain_length = Len(Amount)
decimal_point = 0
For i = 1 To chain_length
character.s = Mid(Amount ,i, 1)
If Not FindString("0123456789.", character)
Amount = Left(Amount, chain_length - 1)
SetGadgetText(1, Amount)
TextLen = Len(Amount)
SendMessage_(GadgetID(1), #EM_SETSEL, TextLen, TextLen)
EndIf
If FindString(".", character)
decimal_point + 1
If decimal_point > 1
Amount = Left(Amount, chain_length - 1)
SetGadgetText(1, Amount)
TextLen = Len(Amount)
SendMessage_(GadgetID(1), #EM_SETSEL, TextLen, TextLen)
EndIf
EndIf
Next i
EndSelect
EndSelect
EndSelect
Until to_leave
Windows only.

Re: Decimal number entry

Posted: Thu Jan 09, 2025 6:13 pm
by jak64
Hello Quin,
Thank you for your response.
I'm on Windows 11 pro with PureBasic v6.12.

I tested your code and it works great!

Thank you for your help

Re: Decimal number entry

Posted: Thu Jan 09, 2025 10:54 pm
by HeX0R
Here is also a (very old, but still working) FloatGadget, if you need it more advanced:
https://www.purebasic.fr/german/viewtop ... 97#p299197
Win only!