The following code demonstrates the above and also copying text between textboxes and totalling the numbers in textboxes to a single textbox.
I hope it's of use. It does not work in Liberty Basic.
Code: Select all
' THIS CODE UTILISES A FUNCTION NumberOnly$() written by Bob Bromley - May 5, 2004
' WHICH I HAVE MODIFIED, BECAUSE I ONLY WANT TO ENSURE NUMERIC ENTRY.
' BOB BROMLEY ALSO PRODUCED CODE THAT UTILISES TRIM$ AND USING TO PRODUCE FORMATTED
' NUMERICAL OUTPUT.
' Thank you Bob for the code you produced.
' The code checks numeric entry for each textbox and then produces it, formatted to
' 2 decimal places in the textbox immediately below. A NOTICE will pop up if a non-
' numeric character is entered and the COMPUTE button pressed.
' The numbers are then listed in a texteditor.
' The COMPUTE button can be pressed after each entry or after all boxes have been
' completed. The downside of the latter, is that the ERROR NOTICE will pop up but
' won't say which box is incorrect.
NOMAINWIN
' SET UP WINDOW SIZE AND POSITION
WindowWidth = 600 : WindowHeight = 630
UpperLeftX = int((DisplayWidth - WindowWidth) / 2)
UpperLeftY = int((DisplayHeight - WindowHeight) / 2)
' SET UP THE CONTROLS
STATICTEXT #m.stat4, "Enter numbers into first four boxes of top row", 35, 0, 400, 20
STATICTEXT #m.stat1, "Unformatted Numbers: Enter number", 35, 15, 290, 20
STATICTEXT #m.stat3, "TOTAL", 355, 15, 75,20
TEXTBOX #m.txt11, 35, 40, 75, 20
TEXTBOX #m.txt12, 110, 40, 75, 20
TEXTBOX #m.txt13, 185, 40, 75, 20
TEXTBOX #m.txt14, 260, 40, 75, 20
TEXTBOX #m.txt15, 335, 40, 75, 20
STATICTEXT #m.stat2, "Formatted Numbers: The numeric results are", 35, 70, 290, 20
TEXTBOX #m.txt21, 35, 90, 75, 20
TEXTBOX #m.txt22, 110, 90, 75, 20
TEXTBOX #m.txt23, 185, 90, 75, 20
TEXTBOX #m.txt24, 260, 90, 75, 20
TEXTBOX #m.txt25, 335, 90, 75, 20
STATICTEXT #m.stat5, "Summary Results", 35, 220, 150, 20
TEXTEDITOR #m.Ted1, 35, 250, 300, 150
BUTTON #m.btn1, "Reset", [ResetAll], UL, 60, 160, 60, 25
BUTTON #m.btn2, "Compute", [Compute], UL, 172, 160, 60, 25
'Open a window for showing the controls and
'entering data
OPEN "Filtered Numeric Input" FOR WINDOW_NF AS #m
#m "trapclose [Quit]"
' I'm not sure of the benefit of the next two lines, because if numbers
' are entered in these boxes they will be overridden by the figures entered
' in the top row, formatted and displayed in the second row and the totals
' calculated and similarly displayed.
'#m.txt15, "!disable"
'#m.txt21, "!disable": #m.txt22, "!disable": #m.txt23, "!disable": #m.txt24, "!disable": #m.txt25, "!disable":
GOTO [ResetStart]
[Compute] ' Format the entered numbers and calculate total
'This routine doesn't quite work. It prints out the results
'correctly in the texteditor, but then prints out a series
'of 0.00 before entering the total.
FOR row = 1 TO 2
FOR col = 1 TO 4
h1$ = "#m.txt";1;col
#h1$ "!contents? amt$"
h2$ = "#m.txt";2;col
amt = CheckDecPoints(amt$) ' Check for one decimal point
amt = NumberOnly(amt$) ' Make the conversion
'#h1$,
num(row,col) = amt
#h2$, (USING("############.##", num(1,col))) ' Put the result in the lower textbox
#m.Ted1, (USING("############.##", num(row,col)))
NEXT
NEXT
total = num(1,1) + num(1,2) + num(1,3) + num(1,4)
amt = total
#m.txt15, amt
#m.txt25, (USING("############.##",amt))
#m.Ted1, (USING("############.##", amt))
WAIT
[ResetAll] ' Blank the textboxes
FOR row = 1 TO 2
FOR col = 1 TO 4
h1$ = "#m.txt";row;col
h2$ = "#m.txt";row;col
#h1$, ""
#h2$, ""
NEXT col
NEXT row
#m.Ted1, "!CLS"
[ResetStart] ' Blank both the textboxes
FOR row = 1 TO 2
FOR col = 1 TO 4
h1$ = "#m.txt";row;col
h2$ = "#m.txt";row;col
a$ = STR$(0)
#h1$, "!CLS"
#h2$, "!CLS"
NEXT col
NEXT row
#m.Ted1, "!CLS"
WAIT
[Quit]
CLOSE #m
END
'----------------------------------------------------------------------------------------
' FUNCTIONS
'----------------------------------------------------------------------------------------
FUNCTION NumberOnly(in$) ' Here is the meat of it!
[Start]
FOR i = 1 TO LEN(in$)
t$ = MID$(in$, i, 1)
[DoItAgain]
SELECT CASE
' Do nothing if t$ is a numeral or a decimal point
CASE ASC(t$) = 46 OR (ASC(t$) > 46) AND (ASC(t$) < 58)
' A minus sign is ok, as long as it preceeds the numbers
CASE ASC(t$) = 45 AND i = 1
CASE ELSE
' Error!! Only valid numbers 0-9, - , and . can be entered
NOTICE "ERROR!!! Please enter a valid number (0-9 - . )"
'GOTO [Start]
'GOTO [DoItAgain]
END SELECT
NEXT
NumberOnly = VAL(in$)
END FUNCTION
' We must allow decimal points, but more than 1 will give us an incorrect return.
' So the first thing to do is count them, and stop here if there is more than 1.
' Use this for severe error-checking.
FUNCTION CheckDecPoints(amt$)
t = 0
FOR i = 1 TO LEN(amt$)
t$ = MID$(amt$, i, 1) : IF t$ = "." THEN t = t + 1
IF t > 1 THEN NOTICE "Only 1 decimal point is allowed in the number! " : WAIT
NEXT
END FUNCTION