## Formatting Numeric Input

Code snippets illustrating how LBB can be used
RNBW
Posts: 61
Joined: Thu Apr 05, 2018 9:21 pm

### Re: Formatting Numeric Input

Has anyone any idea how to rectify the previous post?

Rod
Posts: 9
Joined: Fri Apr 06, 2018 7:00 am

### Re: Formatting Numeric Input

I am not sure I am solving the actual problem but I made a small change to your function to handle -.123 it now returns -0.123

txt\$=".123"
print txt\$,len(txt\$),num\$(txt\$),len(num\$(txt\$))
txt\$="0.123"
print txt\$,len(txt\$),num\$(txt\$),len(num\$(txt\$))
txt\$="-.123"
print txt\$,len(txt\$),num\$(txt\$),len(num\$(txt\$))
txt\$="-0.123"
print txt\$,len(txt\$),num\$(txt\$),len(num\$(txt\$))
end

function num\$(d\$)
t = 0
for x=1 to len(d\$)
a=asc(mid\$(d\$,x,1))
if a = 46 then t = t + 1
if (a = 46) and (t > 1) then a = 0
if a = 45 and x>1 then a = 0
if a = 46 and x = 1 then num\$ = "0" + num\$
'if a = 45 and mid\$(num\$,2,1) = "." then num\$ = "-0" + num\$
if a = 45 or a = 46 or a>47 and a<58 then num\$=num\$+chr\$(a)
' chr\$(46) = "."
next
a=asc(mid\$(num\$,1,1))
if a = 45 and mid\$(num\$,2,1) = "." then num\$ = "-0" + mid\$(num\$,2) '******

end function

RNBW
Posts: 61
Joined: Thu Apr 05, 2018 9:21 pm

### Re: Formatting Numeric Input

RNBW wrote:
Thu May 31, 2018 11:03 am
We seem to have sorted out how to input numeric values into a single textbox with a validity check on each character as it is entered. So my next step is to enable this in a grid of textboxes. I had previously a solution that partially worked (it did not include entering a "0" before a decimal point at the beginning of a number and it truncated the number if you entered a second decimal point).

I used the code for the single textbox solution and it works with the exception of putting a "0" before the decimal point when it appears in a positive or negative number when the decimal point is at the beginning of the number. That statement is not quite correct. If you enter ".123" or "-.123", initially nothing happens. If you go back and edit the numbers by entering two invalid characters in the ".123" number and three invalid characters in the "-.123" number they change to the required "0.123" and "-0.123".

I can't see why it doesn't work in the first place nor why it does work with the adding of invalid characters.

Help please! The code is below:

Code: Select all

``````'===============================================
' FILENAME: NumericInputInAGrid_08.bas
'===============================================

NoMainWin
Row = 3: Col =4
Dim TB\$(Row, Col)
WindowWidth = 500
WindowHeight = 200
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)

bWidth = 100: bHt = 30  'width & height of textboxes

'Sets up the grid of Textboxes
TextboxColor\$ = "white"
print: print
For i = 1 to Row
For j = 1 to Col
Stylebits #main.tb, _ES_RIGHT, _WS_BORDER, 0, 0
TextBox #main.tb, (bWidth)*j -100+20, (bHt)*i - 15, bWidth+1, bHt+1
TB\$(i,j) = "#main.tb"; "_r";i;"c";j
'print TB\$(i,j)   ' prints out handles on main win if you REM nomainwin
maphandle #main.tb, TB\$(i,j)
Next
Next

Open "untitled" For Window As #main
#main, "Font Arial 11"
#main "TrapClose Quit"

While Hwnd(#main)
Scan
For i = 1 To Row
For j = 1 to Col
Call checkInput TB\$(i,j)
Next j
Next i
CallDLL #kernel32, "Sleep", 300   As long, _
ret As void

Wend

'---------------------------------------------------------------
' Check the characters entered.
'---------------------------------------------------------------
Sub checkInput controlName\$
#controlName\$ "!contents? txt\$"
initLen = Len(txt\$)
txt\$ = num\$(txt\$)
If Len(txt\$) < initLen Then
#controlName\$ txt\$
handle = Hwnd(#controlName\$)
pos = Len(txt\$)
CallDLL #user32, "SendMessageA", handle As long, _
_EM_SETSEL As long, _
pos        As long, _
pos        As long, _
result     As void
End If
End Sub

'------------------------
' Close down the program
'------------------------
Sub Quit handle\$
Close #handle\$
End
End Sub

'Valid number check
'-------------------------------------------------------------
function num\$(d\$)
t = 0
for x=1 to len(d\$)
a=asc(mid\$(d\$,x,1))
if a = 46 then t = t + 1
if (a = 46) and (t > 1) then a = 0
if a = 45 and x>1 then a = 0
if a = 46 and x = 1 then num\$ = "0" + num\$
if a = 45 and mid\$(num\$,2,1) = "." then num\$ = "-0" + num\$
if a = 45 or a = 46 or a>47 and a<58 then num\$=num\$+chr\$(a)
' chr\$(46) = "."
next
a=asc(mid\$(num\$,1,1))
if a = 45 and mid\$(num\$,2,1) = "." then num\$ = "-0" + num\$

end function
``````
I've not looked at this for some time, but decided to come back to it. Maybe I might get some inspiration.

Not a chance. I'm still beating my head up against a wall. It might be obvious, but I'm just not getting it. It works until I try to enter .123 or -.123. These entries will allow characters outside the range ".-1234567890" to be entered, whereas other entries such as 1.23 exclude these other characters.

The code for entry into a single textbox is shown below:

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: NumericInputByChar_6
'----------------------------------------------
' Still to resolve: zeros after decimal
' point if it is the last character.
'==============================================

nomainwin
Stylebits #main.textbox, _ES_RIGHT, _WS_BORDER, 0, 0
textbox #main.textbox, 10, 50, 100, 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
for i=1 to len(d\$)
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\$(num\$,1,1))
if a = 45 and mid\$(num\$,2,1) = "." then num\$ = "-0" + num\$

end function

[quit]
close #main   ' close window
end
``````

This works as it should.

I would really appreciate some help on this so I can move on to finish off some code.