Author |
Topic: Numeric entry into a textbox (Read 2311 times) |
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Numeric entry into a textbox
« Thread started on: May 8th, 2016, 1:44pm » |
|
There have been some recent posts on the Liberty Basic Conforums site regarding numeric entry into a textbox and this is a continuation of the subject.
The following code checks for and allows for "-" at the beginning of a number and for ".". It doesn't check for more than one decimal point nor does it check for "." at the end of a number. Numbers cannot be formatted with "," to distinguish thousands, etc.
The code is mainly based on that by Brandon Parker and a code function by GaRPMorE. My own input is very small, amounting to tinkering only, so the credit goes to Brandon Parker and GaRPMorE.
Code:
'==================================================
' NUMERIC INPUT CHECK DEMO
'==================================================
' Originally by Brandon Parker Liberty Basic
' Conforums Re: Error Messege
' « Reply #3 on: Yesterday at 10:47pm »
'--------------------------------------------------
' Modifications by RNBW 7 May 2016
'--------------------------------------------------
' Modification to SLEEP period from 1 to 300
' (allows you to see the unacceptable
' character displayed before deletion.
'--------------------------------------------------
' remchar$() has been replaced by the function
' num$(d$) posted by GaRPMorE in his post
' « Reply #6 on: Apr 22nd, 2016, 7:23pm » and
' modified by RNBW.
' This should enable compatibility with
' earlier versions of LB, including the PRO version
' This version does not appear to be compatible
' with JustBasic
'==================================================
NoMainWin
Dim ControlName$(2)
WindowWidth = 130
WindowHeight = 150
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
TextboxColor$ = "white"
TextBox #main.textbox1, 5, 32, 100, 30
TextBox #main.textbox2, 5, 75, 100, 30
Open "untitled" For Window As #main
#main, "Font Georgia 12"
#main "TrapClose Quit"
ControlName$(1) = "#main.textbox1"
ControlName$(2) = "#main.textbox2"
While Hwnd(#main)
Scan
For i = 1 To 2
Call checkInput ControlName$(i)
Next i
CallDLL #kernel32, "Sleep", 300 As long, _
ret As void
Wend
'---------------------------------------------------------------
' Check the characters entered.
'---------------------------------------------------------------
' It does not check for more than one occurence of "."
' or that "." is not the last character.
'---------------------------------------------------------------
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$)
for i=1 to len(d$)
a=asc(mid$(d$,i,1))
if a = 45 and i = 1 or a = 46 or a>47 and a<58 then num$=num$+chr$(a)
next
end function
What would be useful is if someone could come up with the code to check for more than one "." and for dealing with "." at the end of a number.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #1 on: May 8th, 2016, 2:45pm » |
|
on May 8th, 2016, 1:44pm, RNBW wrote:What would be useful is if someone could come up with the code to check for more than one "." |
|
Easy:
Code:function HasMoreThanOneDot(n$)
HasMoreThanOneDot = instr(n$, ".", instr(n$, ".") + 1) <> 0
end function Quote:and for dealing with "." at the end of a number. |
|
It's debatable whether that should be rejected. If .123 is a valid number why shouldn't 123. be too? Certainly many languages (including LBB) accept is as valid, although LB doesn't. It's trivial to test for if you want to, especially if the LB 4.5.0 endswith() function is available:
Code:function EndsWithADot(n$)
EndsWithADot = endswith(n$, ".")
end function Richard.
|
|
Logged
|
|
|
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: Numeric entry into a textbox
« Reply #2 on: May 8th, 2016, 3:37pm » |
|
Hi Richard
Thank you for your response.
I'm not too bothered about the "." at the end of the number. I'd be going on to format the number to "###" or whatever decimal places anyway.
Your code for the more than one "." is what I am really looking for. It might be because it's Sunday and the sun is shining, but I can't for the life of me think how to incorporate it into the code.
Ray
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #3 on: May 8th, 2016, 4:07pm » |
|
on May 8th, 2016, 3:37pm, RNBW wrote:I can't for the life of me think how to incorporate it into the code. |
|
The code you listed does not check the entered number for validity, rather it filters the entered number to remove invalid characters. So it's performing an entirely different task.
You can't use a similar approach in the case of having more than one dot, because there's no way the program can determine which is the 'wanted' dot and which the 'unwanted' one. Only the user knows that.
I answered the question you asked: What would be useful is if someone could come up with the code to check for more than one "." but it wasn't the right question!
Richard.
|
|
Logged
|
|
|
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: Numeric entry into a textbox
« Reply #4 on: May 8th, 2016, 4:23pm » |
|
Hi Richard
Of course you are correct. What I was hoping for was someone to come up with code that would carry out the check as each character is entered.
I'd come to the same conclusion as yourself that the function didn't achieve that. So, unless someone can come up with some clever code, I shall have to carry out a further check after the whole number has been entered.
Thanks for your help
Ray
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #5 on: May 8th, 2016, 6:58pm » |
|
on May 8th, 2016, 4:23pm, RNBW wrote:What I was hoping for was someone to come up with code that would carry out the check as each character is entered. |
|
Ah, so it's always the first decimal point typed that is the correct one? If you somehow know that to be the case then of course it's trivial to reject a second one. That's not the same thing as checking for more than one dot, since the filtering will ensure there never can be.
Richard.
|
|
|
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: Numeric entry into a textbox
« Reply #6 on: May 9th, 2016, 07:34am » |
|
Point taken!
So the only safe way to do it is to check the final entered number and if there is more than one "." then to give a warning to that effect and let the user decide which is the correct "." and re-enter the number.
|
« Last Edit: May 9th, 2016, 07:34am by RNBW » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #7 on: May 9th, 2016, 09:43am » |
|
on May 9th, 2016, 07:34am, RNBW wrote: Would that be "decimal point" taken?
Richard.
|
|
Logged
|
|
|
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: Numeric entry into a textbox
« Reply #8 on: May 18th, 2016, 10:20am » |
|
I have tidied up the routine a little: * increased the number of textboxes from 2 to 3 * instead of separate textboxes, made them into a grid * made the borders thinner * made the entry right justified (like calculator entry)
It still needs the issue of more than one decimal point to be dealt with. I think this will have to be a Notice with a warning for the user to ensure just one decimal point.
Here's the code to date. Hope it's of use.
Code:
'==================================================
' NUMERIC INPUT CHECK DEMO
'==================================================
' Originally by Brandon Parker Liberty Basic
' Conforums Re: Error Messege
' « Reply #3 on: Yesterday at 10:47pm »
'--------------------------------------------------
' Modifications by RNBW 7 May 2016
'--------------------------------------------------
' Modification to SLEEP period from 1 to 300
' (allows you to see the unacceptable
' character displayed before deletion.
'--------------------------------------------------
' remchar$() has been replaced by the function
' num$(d$) posted by GaRPMorE in his post
' « Reply #6 on: Apr 22nd, 2016, 7:23pm » and
' modified by RNBW.
' This should enable compatibility with
' earlier versions of LB, including the PRO version
' This version does not appear to be compatible
' with JustBasic
'==================================================
NoMainWin
NumOfTB = 3
Dim ControlName$(NumOfTB)
WindowWidth = 130
WindowHeight = 250
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
bWidth = 100
bHt = 30
TextboxColor$ = "white"
Stylebits #main.textbox1, _ES_RIGHT, _WS_BORDER, 0, 0
TextBox #main.textbox1, 5, 30, bWidth, bHt+1
Stylebits #main.textbox2, _ES_RIGHT, _WS_BORDER, 0, 0
TextBox #main.textbox2, 5, 60, bWidth, bHt+1
Stylebits #main.textbox3, _ES_RIGHT, _WS_BORDER, 0, 0
Textbox #main.textbox3, 5, 90, bWidth, bHt+1
Open "untitled" For Window As #main
#main, "Font Georgia 12"
#main "TrapClose Quit"
'ControlName$(1) = "#main.textbox1"
'ControlName$(2) = "#main.textbox2"
'ControlName$(3) = "#main.textbox3"
For i = 1 to NumOfTB
ControlName$(i) = "#main.textbox" + str$(i)
next
While Hwnd(#main)
Scan
For i = 1 To NumOfTB
Call checkInput ControlName$(i)
Next i
CallDLL #kernel32, "Sleep", 300 As long, _
ret As void
Wend
'---------------------------------------------------------------
' Check the characters entered.
'---------------------------------------------------------------
' It does not check for more than one occurence of "."
' or that "." is not the last character.
'---------------------------------------------------------------
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$)
for i=1 to len(d$)
a=asc(mid$(d$,i,1))
if a = 45 and i = 1 or a = 46 or a>47 and a<58 then num$=num$+chr$(a)
next
end function
|
|
Logged
|
|
|
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: Numeric entry into a textbox
« Reply #9 on: May 19th, 2016, 2:01pm » |
|
I am trying to extend the use of Brandon Parker's code for numeric entry. In my last post I produced code for a small grid consisting of 3 textboxes.
This worked pretty well, so I thought I would try a 3 x 3 grid.
I just can't get it to work.
Code:
NoMainWin
Row = 3: Col =3
Dim TB$(Row, Col)
WindowWidth = 240
WindowHeight = 200
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
bWidth = 100: bHt = 30 'width & height of textboxes
TextboxColor$ = "white"
Stylebits #main.tb1, _ES_RIGHT, _WS_BORDER, 0, 0
TextBox #main.tb1, 20, 30, bWidth, bHt+1
Stylebits #main.tb2, _ES_RIGHT, _WS_BORDER, 0, 0
TextBox #main.tb2, 20, 60, bWidth, bHt+1
Stylebits #main.tb3, _ES_RIGHT, _WS_BORDER, 0, 0
Textbox #main.tb3, 20, 90, bWidth, bHt+1
Open "untitled" For Window As #main
#main, "Font Arial 11"
#main "TrapClose Quit"
For i = 1 to Row
For j = 1 to Col
TB$(i,j) = "#main.tb" + str$(i) + str$(j)
Next
Next
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.
'---------------------------------------------------------------
' It does not check for more than one occurence of "."
' or that "." is not the last character.
'---------------------------------------------------------------
Sub checkInput controlName$
#controlName$ "!contents? txt$" 'THIS IS WHERE THE ERROR COMES UP
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$)
for i=1 to len(d$)
a=asc(mid$(d$,i,1))
if a = 45 and i = 1 or a = 46 or a>47 and a<58 then num$=num$+chr$(a)
next
end function
I have marked the line in the code where the error is reported (Invalid channel at line 72).
I don't seem to be able to get my head around it. Has anybody any ideas?
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #10 on: May 19th, 2016, 3:41pm » |
|
on May 19th, 2016, 2:01pm, RNBW wrote:I don't seem to be able to get my head around it. Has anybody any ideas? |
|
I am surprised that this should have given you difficulty. The error occurs in this line:
Code: #controlName$ "!contents? txt$" so it's clear that the variable controlName$ doesn't contain a valid handle. The next step in debugging is to find out what it does contain, which you can easily do either by printing it to the MAINWIN or using the debugger. What you will find is this:
Code:controlName$ = "#main.tb11" Now you can see why the error occurs: there is no control with the handle #main.tb11!
Here is a corrected version of the program which does not raise an error (LBB only of course):
Code: NoMainWin
Row = 3: Col =3
Dim TB$(Row, Col)
WindowWidth = 400
WindowHeight = 200
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
bWidth = 100: bHt = 30 'width & height of textboxes
TextboxColor$ = "white"
For i = 1 to Row
For j = 1 to Col
Stylebits #main.tb, _ES_RIGHT, _WS_BORDER, 0, 0
TextBox #main.tb, (bWidth+20)*j - 100, (bHt+10)*i - 15, bWidth, bHt+1
TB$(i,j) = "#main.tb" + str$(i) + str$(j)
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.
'---------------------------------------------------------------
' It does not check for more than one occurence of "."
' or that "." is not the last character.
'---------------------------------------------------------------
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$)
for i=1 to len(d$)
a=asc(mid$(d$,i,1))
if a = 45 and i = 1 or a = 46 or a>47 and a<58 then num$=num$+chr$(a)
next
end function Richard.
|
|
Logged
|
|
|
|
RNBW
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: Numeric entry into a textbox
« Reply #11 on: May 19th, 2016, 4:07pm » |
|
Thank you Richard for the corrected code. I'm afraid the blank look on my face is a bit more frequent these days. I could see what the problem was, the blank bit was nothing happening between the ears to solve it.
I thought I should have maphandle in there, but I didn't need it when I was using just a single dimension array. so I thought I was missing something, which clearly I was.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #12 on: May 19th, 2016, 5:18pm » |
|
on May 19th, 2016, 4:07pm, RNBW wrote:I thought I should have maphandle in there, but I didn't need it when I was using just a single dimension array. |
|
In your 1D version you created the textboxes individually, rather than in a loop (with only three that wasn't too onerous) so you didn't need MAPHANDLE. But with a 2D grid of boxes it's much easier to create them using a nested loop, and increasingly so when the number of rows or columns increases.
As has been discussed here before, as soon as the number of rows and columns exceeds 10 you have to be a bit more careful about constructing the handles. The simple
Code:TB$(i,j) = "#main.tb" + str$(i) + str$(j) won't cut it in that case because (for example) i = 11, j = 1 would give exactly the same handle as i = 1, j = 11!
Richard.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Numeric entry into a textbox
« Reply #13 on: May 19th, 2016, 8:20pm » |
|
on May 19th, 2016, 5:18pm, Richard Russell wrote:But with a 2D grid of boxes it's much easier to create them using a nested loop, and increasingly so when the number of rows or columns increases. |
|
This is of course an advantage that LBB has over LB, but it seems that nobody dares mention that at the LB Community Forum, where there is currently a parallel thread (despite it having been stated in the past that such a reference to LBB is allowed).
Richard.
|
|
Logged
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: Numeric entry into a textbox
« Reply #14 on: May 20th, 2016, 08:00am » |
|
Just in case it isn't obvious leading zeroes is one solution.
Code:dim TB$(11,11)
j=1
i=11
TB$(i,j) = "#main.tb" + str$(i) + str$(j)
print TB$(i,j)
TB$(i,j) = "#main.tb" + right$("00"+str$(i),2) + right$("00"+str$(j),2)
print TB$(i,j)
|
|
Logged
|
|
|
|
|