Decimal number entry

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Decimal number entry

Post 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
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Decimal number entry

Post 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.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Decimal number entry

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Decimal number entry

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