It is currently Sun Jul 15, 2018 10:28 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Numeric Entry into a EditBox
PostPosted: Wed Jul 11, 2018 3:43 pm 
Offline
User
User

Joined: Thu Jan 02, 2014 5:01 pm
Posts: 46
I have been trawling through the forum for examples of numeric input into a textbox. What I wanted was the following:
Check each character as it is entered to ensure that:

• The character was limited to 0 to 9, minus and decimal point (period)
• Minus can only occur as the first character entered
• There can only be one occurrence of minus
• There can only be one occurrence of a decimal point
• If the first character entered is a decimal point (.123) it will be displayed as 0.123
• If the first character is minus and the second a decimal point (-.123) it will be displayed as -0.123
• The display will be right justified
• It must be editable

I couldn’t find code that did all of these, but was able to find two that came close. I have amalgamated the two. Credit is given in the code to the writers of the original code. I am very grateful to them and I hope that the code below will be useful. I shall be grateful if anyone can improve it or put me right if I have got anything wrong.

Code:
;--------------------------------------------------------------------------------------------
;  Numeric input
;---------------------------------------------------------------------------------------------
;  This is an amalgamation of two programs.  One from Danilo
;  included in RSBasic's WinAPI Library (StringGadget section)
;  and BasicallyPure's program in the Purebasic forum
;  (https://www.purebasic.fr/english/viewtopic.php?f=13&t=50565&hilit=Numeric+entry&start=0)
;  It accepts characters 0 to 9 minus and period.
;  It accepts a minus only as the first character.
;  It only accepts one minus sign and one period in the entry.
;  An entry of .123 displays as 0.123
;  An entry of -.123 is displayed as -0.123
;  The amalgamation of code has been produced by RNBW, who is grateful
;  to all the contributors to the code:
;  Danilo
;  BasicallyPure
;  asked for by 'akee'
;  inspired by 'IdeasVacuum'
;  key assist from 'Shardik'
;---------------------------------------------------------------------------------------------

EnableExplicit

Procedure.s CheckNumeric(iStrGadget.i)
   ;purpose: limit StringGadget to accept only numeric input
   ;with support for floating poing characters: . - + E e
   
   Protected.i iCount, iTextLength, iAccept, iDecimal, iExp, iNeg
   Protected.s sNewText, sChar, sGadgetText
   
   sGadgetText = GetGadgetText(iStrGadget)
   iTextLength = Len(sGadgetText)
   
   For iCount = 1 To iTextLength
     
      sChar = Mid(sGadgetText, iCount, 1)
      iAccept = #False
     
      Select Asc(sChar)
         Case 48 To 57 ; Accepted numbers
            iAccept = #True
            If iExp = 1 : iExp + 1 : EndIf
         Case 43, 45        ; Minus or Plus sign
            If iCount = 1
               iAccept = #True : iNeg = 1
            ElseIf iExp = 1
               iAccept = #True : iExp + 1
            EndIf
         Case 46        ; period
            If Not iDecimal And Not iExp
               iDecimal = #True : iAccept = #True
               If iCount - iNeg = 1 : sChar = "0" + sChar : EndIf
            EndIf
         Case 69, 101 ;    'e' or 'E'
            If Not iExp And iCount > 1 + iNeg
               iExp = 1 : iAccept = #True
            EndIf
      EndSelect
     
      If iAccept : sNewText + sChar : EndIf
     
   Next
   
   SetGadgetText(iStrGadget, sNewText)
   iTextLength = Len(sNewText)
   
   ;windows only
   SendMessage_(GadgetID(iStrGadget),#EM_SETSEL,iTextLength,iTextLength) ;Set cursor to end of string

EndProcedure

If OpenWindow(0,0,0,300,150,"Number of Decimal Places",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  StringGadget(1,10,10,200,20,"",#PB_Text_Right)
  LoadFont (1, "Monaco", 11, #PB_Font_Bold)
  SetGadgetFont(1, FontID(1))
 
  Repeat
    Define EventID=WaitWindowEvent()
    If EventID=#PB_Event_Gadget
      Select EventGadget()
        Case 1
          If EventType() = #PB_EventType_Change
            CheckNumeric(EventGadget())
          EndIf
      EndSelect
    EndIf
    If EventID = #PB_Event_CloseWindow
      End
    EndIf
  ForEver
EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: Numeric Entry into a EditBox
PostPosted: Thu Jul 12, 2018 7:35 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3281
Location: New Zealand
seems to work well, but not "e" exp, needs a bit more thought
as you can type 123.456e1234
the exponent needs to be limited in the range, for float +/- 38 or double +/- 308
so if you have "1." entered you can flag it as a potential of exp and if "e" is entered then range check the numbers after to be
within +/- 38 for float or +/- 308 for double
not sure how to do that right now but will look at it in the morning

_________________
Got winter blues?
Enjoy a Caravan Trip into, "The Land of Grey and Pink", wine and punk weed optional!
https://www.youtube.com/watch?v=9hmFzGTxod4


Top
 Profile  
Reply with quote  
 Post subject: Re: Numeric Entry into a EditBox
PostPosted: Fri Jul 13, 2018 11:39 am 
Offline
User
User

Joined: Thu Jan 02, 2014 5:01 pm
Posts: 46
idle wrote:
seems to work well, but not "e" exp, needs a bit more thought
as you can type 123.456e1234
the exponent needs to be limited in the range, for float +/- 38 or double +/- 308
so if you have "1." entered you can flag it as a potential of exp and if "e" is entered then range check the numbers after to be
within +/- 38 for float or +/- 308 for double
not sure how to do that right now but will look at it in the morning


You are right that the incorporation of the entry of numbers such as 123.456e1234 (or more importantly 123.456e-1234) needs more thought. With what I had in mind, I wouldn't require such numbers to actually be entered.

One of the problems is that the code as written only allows for a minus sign to be the first character entered and only one minus sign is permitted. The e (or E) shouldn't be too much of a problem to change, but you couldn't enter 123e-12 or -123e-12, the first because of the rule I've just mentioned and the latter with a double no-no because it has a minus in the middle of the entry and also two minus signs.

These problems have to be overcome and it's not easy. I must say, if I was going to need to enter a 123e-12 type number, I would probably use a completely different method, such as entering the whole number and then carrying out a check on its validity, or alternatively enter a number without the e-12 part and identify that with a label after the textbox and deal with the number itself in code outside the entry. Personnally, I would favour the latter method. It's much easier to deal with.

I currently don't have time to look into it, but if anyone could come up with solutions I would like to see them.

Personnally, my next step is to produce a grid of textboxes with numeric entry into them. If anyone has any ideas, it would save my poor old brain a few headaches!


Top
 Profile  
Reply with quote  
 Post subject: Re: Numeric Entry into a EditBox
PostPosted: Fri Jul 13, 2018 1:30 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Oct 06, 2006 3:57 pm
Posts: 474
Location: England
RNBW wrote:
idle wrote:
Personnally, my next step is to produce a grid of textboxes with numeric entry into them. If anyone has any ideas, it would save my poor old brain a few headaches!


I use a scroll areagadget and an list (array) of stringgadgets for my entrirley PB based grid gadget - but it's a bit of a bodge :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Numeric Entry into a EditBox
PostPosted: Fri Jul 13, 2018 2:19 pm 
Offline
User
User

Joined: Thu Jan 02, 2014 5:01 pm
Posts: 46
I must be going daft (ask my wife she'll probably confirm that - she's being saying it for years). The code DOES allow the input of numbers such as 123e24 and -123e-24. I found I was checking it out using old code, which didn't allow it because I had stripped out the incorporation of 'e'. I decided to include it before I posted the code.


Top
 Profile  
Reply with quote  
 Post subject: Re: Numeric Entry into a EditBox
PostPosted: Fri Jul 13, 2018 5:09 pm 
Offline
User
User

Joined: Thu Jan 02, 2014 5:01 pm
Posts: 46
captain_skank wrote:
RNBW wrote:
idle wrote:
Personnally, my next step is to produce a grid of textboxes with numeric entry into them. If anyone has any ideas, it would save my poor old brain a few headaches!


I use a scroll areagadget and an list (array) of stringgadgets for my entrirley PB based grid gadget - but it's a bit of a bodge :)


Is your grid gadget on the forum?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye