TextBox limit number of digit to enter

mike2ppc

Member
Licensed User
Longtime User
Hello,

does somebody know to limit in a TextBox possible digits
e.g.

Form1 - TextBox1

Sub TextBox1_KeyPress (key)
If StrLength(textbox1.Text)>3 Then
key=""
End If
End Sub

I want to limit the Textbox to max. 3 digits, is this possible.

Best Regards

Michael
 

taximania

Well-Known Member
Licensed User
Longtime User
This was not done by me.
I think it's agraham who wrote this code.
I think the number 2 is correct for your needs.

B4X:
Sub TextBox1_KeyPress (key)
    If StrLength( textbox1.Text) >=[COLOR="Red"]2[/COLOR] Then   
        If Asc(key)>31 Then
            ss = Textbox1.SelectionStart
            Textbox1.Text= SubString(Textbox1.Text,0,[COLOR="red"]2[/COLOR])
            Textbox1.SelectionStart = ss
        End If
    End If
End Sub
 

specci48

Well-Known Member
Licensed User
Longtime User
If you want to do it a more elegant way, just have a look at filippos fgcontrols library.

The fgTextBox provides a MaxLenght and a OnlyNumber property.


specci48
 

mike2ppc

Member
Licensed User
Longtime User
Thanks for all the idea,
my final solution is now

On Form1 are 2 TextBox Controls:

Sub App_Start
AddEvent("TextBox1",KeyPress,"TextBox_maxC5_KeyPress")
AddEvent("TextBox2",KeyPress,"TextBox_maxC5_KeyPress")
Form1.Show
End Sub


Sub TextBox_maxC5_KeyPress (key)
If StrLength(Control(Sender))=5 AND _
( IsLetter(key) OR IsNumber(key) ) Then
Control(Sender).IgnoreKey
End If
End Sub
 
Top