I've managed to change the code so that it now ensures that there is only one period "." in the number. The only areas of validation that it doesn't cover are exponential numbers (which personally I'm not bothered about), which someone may wish to include and also the issue of "." at the end of a number. It would be nice to put one or two zeros after this, but I don't seem to be able to get my head around it. Perhaps someone could help here.RNBW wrote: ↑Thu Apr 12, 2018 5:18 pm This is a bit more advanced than the previous examples. The program sets up a grid into which you can enter numbers ( - . 0 to 9 ). It carries out a check as you enter each individual character. If it's a valid number it accepts it. If it's not valid, you will see the character very briefly before it's deleted. Press tab to move to next cell to right and Shift+Tab to move to cell to left.
I have produced the code for a grid of textboxes, but if someone wants to simplify it and produce code say for one textbox that may be helpful to beginners.
Code: Select all
'===============================================
' FILENAME: NumericInputInAGrid_03.bas
'-----------------------------------------------
'Currently this works fine including restricting
'to only one minus sign "-" at the start of the
'number and one period ".".
'===============================================
NoMainWin
Row = 10: Col =9
Dim TB$(Row, Col)
WindowWidth = 1000
WindowHeight = 400
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
'-------------------------------------------------------------
' function to replace remchar$() LB4.5 function
' This should make this compatible with earlier version of LB
' Code by GaRPMorE in Re: textbox input - numbers only
' in LB Forum Reply #6 on: Apr 22nd, 2016, 7:23pm »
'-------------------------------------------------------------
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 t > 1 then a = 8
if a = 45 and i = 1 or a = 46 or a>47 and a<58 then num$=num$+chr$(a)
' chr$(46) = "."
next
end function