Numeric Entry Into A Textbox

Discussions about the Liberty BASIC language, with particular reference to LB Booster
RNBW
Posts: 66
Joined: Thu Apr 05, 2018 9:21 pm

Numeric Entry Into A Textbox

Post by RNBW »

It's some while since I submitted code for Numeric Entry Into A Textbox (and I can't find it in the forum). However, I have produced an update, which Is submitted below. Basically, the routine allows a textbox to be opened and the user can then enter only the characters 0-9 and - (comma) and . (decimal point). It also has a restriction on the total number of characters that can be entered (same range as abov). In my code I have made the maximum number of characters as 12, but this can be changed by the user by changing the value of maxNumChars.
I hope it is a useful contribution.

Code: Select all

'==============================================
' Numeric entry into a textbox.
' This permits the entry of numbers including
' "-" and ".".  It also prevents more than one
' "-" and "." from being entered.
' Filename: NumericInputInTextboxByChar_8
'--------------------------------------------------------------------------------'==============================================

nomainwin
Stylebits #main.textbox, _ES_RIGHT, _WS_BORDER, 0, 0
textbox #main.textbox, 10, 50, 150, 25
WindowWidth = 350
WindowHeight = 150

open "Filtered Numeric Input" for window as #main
print #main, "Font Arial 12 Bold"
#main.textbox "!contents? txt$"
#main.textbox "!setfocus"
#main "trapclose [quit]"

[CheckInput]
timer 0

    #main.textbox "!contents? txt$"   'text in textbox
     oldtxt$ = txt$    ' make text the oldtext
    txt$ = num$(txt$)  ' gets new text from function num$(text$) which does the checking
    if oldtxt$ <> txt$ then
        #main.textbox txt$      ' if old text is not the same as the new text then use new text
        handle = hWnd(#main.textbox)   ' get the handle of the textbox
        pos = len(txt$)    ' position of character is the length of the current text
        calldll #user32, "SendMessageA", _   ' call EM_SETSEL
            handle as ulong, _
            _EM_SETSEL as long, _
            pos as long, _
            pos as long, _
            result as void
    end if

timer 5, [CheckInput]  ' short delay then last char is deleted if not 0-9, - or .
wait   ' wait for event

' Function to check that character input is 0-9, - or .
function num$(d$)
    t = 0
    maxNumChars = 12
    for i=1 to maxNumChars 'input of characters will stop at 12 (including 0-9, - and .
        a=asc(mid$(d$,i,1))
        if a = 46 then t = t + 1
        if (a = 46) and (t > 1) then a = 0    'only DOT after FIRST is cleared
        if a = 45 and i>1 then a = 0          'so really almost anything do, not just 8
        if a = 46 and i = 1 then num$ = "0" + num$
        if (a = 45) or (a = 46) or (a>47 and a<58) then num$=num$+chr$(a)
    next
    a=asc(mid$(d$,1,1))
    if a = 45 and mid$(d$,2,1) = "." then num$ = "-0" + num$
end function

[quit]
close #main   ' close window
end