How to remove decimal

jschuchert

Active Member
Licensed User
Longtime User
I want to prevent a user entering a decimal in a text box using the keypress event. For example, he enters 123. and then a message box says no decimals allowed. When the message box is closed I only want 123 remaining in the text box. I have tried several solutions but the decimal never goes away. I can probably make it work in the lostfocus event but I want the msgbox to appear when the "." is pressed. I don't mind if the text box is even totally cleared.

Here is the code representing several scenarios:

Sub txtAngDist_KeyPress (key)
If key = "." Then
Msgbox("Whole numbers only (no decimals) during the evaluation")
txtangdist.Text = "" 'doesn't clear the "." but don't know why
txtangdist.text=strreplace(txtangdist.text,".","") ' doesn't work
txtangdist.text= txtangdist.Text & "." 'otherwise the strlength stops at the "."
len=StrLength(txtangdist.Text )
txtangdist.Text =StrRemove(txtangdist.Text ,len-1,1) 'doesn't work
End If
End Sub

I tried several other combinations but still can't solve it. It's probably a very simple solution and I will feel like an idiot when I am shown but it won't be the first time. Thanks for your help.

Jim Schuchert
 

tsdt

Active Member
Licensed User

Maybe should clear the "key" parameter to "" instead of the text box. At the point, I think the textbox would not have the "." yet. If you clear the "key", will that help?

Tested.. This is not working.. Sorry for the wrong suggestion.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
This routine will do it.
I added the MessageBox according to your question but personally I would remove it.
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]txtangdist_KeyPress (key)[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff] If [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Asc[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](key)<[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]48 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]OR [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Asc[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](key)>[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]57 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]   Msgbox[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Invalid character, only 0 to 9"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"ERROR"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]cMsgboxOK[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]cMsgboxExclamation[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]   txtangdist.IgnoreKey[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff] End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=Courier New][COLOR=#0000ff]End Sub[/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

Best regardas
 
Last edited:

jschuchert

Active Member
Licensed User
Longtime User
Thanks for trying, tsdt. I know the feeling.

Klaus, once again you have come to my rescue with a perfect solution. I used to use this type of coding in my VB programs but never thought of it here. Incidentally, why didn't any of my ideas work? Is it because of using the 'keypress' event?

Jim
 

klaus

Expert
Licensed User
Longtime User
If I remember well, entering a character in TextBox works like this:
- KeyDown event is raised available with the Door library
- KeyUp event is raised available with the Door library
- KeyPress event is raised, the text is not yet changed available to B4PPC
- TextChanged event is raised, the text is changed but not yet displayed available with the Door library
- the text is displayed

Perhaps I have forgotten something, but that's how I understand it.

Attached a small sample program with the Door library changing automatically the text to uppercase using the TextChanged event.

Best regards.
 

Attachments

  • UpperCase.sbp
    801 bytes · Views: 196
Cookies are required to use this site. You must accept them to continue using the site. Learn more…