Tasp
Full Member
Posts: 215
|
Post by Tasp on Sept 10, 2023 12:23:22 GMT -5
I'm sure previously I have created a textbox that stops the user from using special characters ,.<>?/\:@{:!"£$%^&*() etc. And I'm sure this is a stylebits attribute. But for the life of me I cannot find it anywhere. Can anyone remember seeing this? I thought it was alycesrestaurant.com/lbpe/Stylebits%20-%20Textboxes.html
|
|
|
Post by Brandon Parker on Sept 10, 2023 14:50:36 GMT -5
Unfortunately, one can not limit a TextBox in Windows to only A-Z & Numbers. You can limit a TextBox to numbers only very easily with Stylebits (although even that is limited to typing; pasting still allows letters to be entered... ). There are a few ways to approach this issue: 1. Continuously check in a loop what the text in the TextBox is, and if a character is not allowed, you can notify the user. 2. Check after the user submits the data and notify the user if there is an issue. 3. Use an external DLL to trap Windows messages and detect which character is being inserted into the TextBox. If this is a password or "hidden" character TextBox (i.e. _ES_PASSWORD has been set) then automatically removing characters is a bad idea since the user might not notice if you do not halt the program and show a warning which could result in an unwanted entry/password being set. One nice thing to do would be to add a ToolTip to the TextBox which has a short notice stating which characters are not allowed. {:0) Brandon Parker
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Sept 11, 2023 12:08:22 GMT -5
Yeah, this is not for a password box, I know it's easy to parse the textbox to allow only only what I allow. But I'm convinced I have found a way to do this with just stylebits, in a previously project. But for the life of me I cannot find it! I'm sure when you try to use special char a 'tooltip' pops up and prevents the user from actually type them. This is a windows default pop up.
|
|
|
Post by Rod on Sept 11, 2023 13:21:00 GMT -5
The LBPE has a stylebits demo which limits input to numeric characters only. But it does not allow +- or . So you have to have two input boxes if you want decimal and probably a + or - input box if negative numbers are require
|
|
Sver
Full Member
Posts: 145
|
Post by Sver on Nov 5, 2023 3:52:18 GMT -5
Remove wrong letters immediately. (without using remchar$)
nomainwin
textbox #main.textbox, 10, 50, 100, 25 WindowWidth = 350 WindowHeight = 150 open "Filtered Numeric Input" for window as #main #main.textbox "!contents? txt$" #main "trapclose [quit]"
[CheckInput] timer 0
#main.textbox "!contents? txt$" Symbols$ = "abcdefghijklmnopqrstuvwxyz";_ "ABCDEFGHIJKLMNOPQRSTUVWXYZ<>,:;!@#$%^&*()_=+`~" oldtxt$=txt$ ' txt$ = remchar$( txt$ , Symbols$) txt$ = num$(txt$) 'replacement for remchar$()
if oldtxt$<> txt$ then #main.textbox txt$ handle = hWnd(#main.textbox) pos = len(txt$) calldll #user32, "SendMessageA", _ handle as ulong, _ _EM_SETSEL as long, _ pos as long, _ pos as long, _ result as void end if
timer 300, [CheckInput] 'or shorter wait
function num$(d$) for i=1 to len(d$) a=asc(mid$(d$,i,1)) ' if a = 45 or a = 46 or a>47 and a<58 then num$=num$+chr$(a) if a=83 or a=109 then num$=num$+chr$(a) 'only letter S en m next end function
[quit]
close #main end
|
|
rnbw
New Member
Posts: 48
|
Post by rnbw on Nov 5, 2023 6:29:21 GMT -5
The LBPE has a stylebits demo which limits input to numeric characters only. But it does not allow +- or . So you have to have two input boxes if you want decimal and probably a + or - input box if negative numbers are require nomainwin ' Add Stylebits to make entered text right justified ' in digital calculator style 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 #main.textbox "!contents? txt$" #main "trapclose [quit]"
[CheckInput] timer 0
#main.textbox "!contents? txt$" 'Symbols$ = "abcdefghijklmnopqrstuvwxyz";_ '"ABCDEFGHIJKLMNOPQRSTUVWXYZ<>,:;!@#$%^&*()_=+`~" oldtxt$=txt$ ' txt$ = remchar$( txt$ , Symbols$) txt$ = num$(txt$) 'replacement for remchar$()
if oldtxt$<> txt$ then #main.textbox txt$ handle = hWnd(#main.textbox) pos = len(txt$) calldll #user32, "SendMessageA", _ handle as ulong, _ _EM_SETSEL as long, _ pos as long, _ pos as long, _ result as void end if
timer 300, [CheckInput] 'or shorter wait
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) 'for i=1 to len(d$) 'a=asc(mid$(d$,i,1)) 'if a = 45 or a = 46 or a>47 and a<58 then num$=num$+chr$(a) 'if a=83 or a=109 then num$=num$+chr$(a) 'only letter S en m next ' If first character is a "." place "0" before the decimal point. ' If first character is "-" and second character is "." place ' "0" before the decimal point. a=asc(mid$(num$,1,1)) if a = 45 and mid$(num$,2,1) = "." then num$ = "-0" + num$ end function
[quit]
close #main end
Basically, you can write code that will only allow what you want to be shown in the textbox as Sver was doing by only allowing "m" to be shown. If you increase the value of timer you can see the character that you have unsuccessfully tried to enter.
|
|
|
Post by Rod on Nov 5, 2023 7:45:18 GMT -5
Yes, that works well. The original question was about a stylebits solution. I think we are still searching for that.
|
|
rnbw
New Member
Posts: 48
|
Post by rnbw on Nov 5, 2023 9:59:59 GMT -5
Rod I don't think there is a Stylebits solution. I don't believe that Stylebits is designed to do that. I couldn't find anything in the Windows API either. I think the way to do it is by writing code for it similar to that provided by Sver.
|
|
|
Post by Rod on Nov 5, 2023 10:54:19 GMT -5
Yes, I agree. The stylebits solution that is available has too many compromises and a timed read allows all sorts of input management.
|
|
rnbw
New Member
Posts: 48
|
Post by rnbw on Nov 5, 2023 11:10:20 GMT -5
Rod, It's not perfect, but a little bit of code that excludes the characters that Tasp listed. It works on the basis of only allowing the characters that you want to allow to be entered and excludes all others.
'============================================== ' Limit characters to a range you want to be ' entered and exclude all others. ' In this case, permitted characters are: ' a to z, A to Z, 0 to 9, minus, plus, space ' and comma ' All permitted characters can be entered ' without restriction. '==============================================
nomainwin Stylebits #main.textbox, _ES_LEFT, _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 for i=1 to len(d$) a=asc(mid$(d$,i,1)) t = t + 1 if a = 32 or (a > 42 and a <47) or (a > 47 and a<58) or (a > 64 and a < 91) or (a > 96 and a < 123) then num$ = num$ + chr$(a) next end function
[quit] close #main ' close window end
|
|