Limiting Text Entry Length

jireland

Member
Licensed User
Is there a way of limiting the length of text entry?
I want the User to enter a single charachter in a text box of only one character wide. I have tried the TextBox.Selection length, but without success.
I must be doing something wrong, but not sure what.....
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the KeyPress event:
B4X:
Sub TextBox1_KeyPress (key)
    If StrLength(TextBox1.Text) >= 1 AND Asc(key) <> 8 Then TextBox1.IgnoreKey
End Sub
Asc(key) <> 8 - Always allow the BackSpace key.

You can also use agrham's ControlEvents library and catch the TextChanged event.
Inside this event check the length of TextBox.Text and trim it if necessary.
http://www.b4x.com/forum/showthread.php?t=934
 
Top