Hello all, I have been trying to limit text input in the TextField but failed. And here are my codes :
B4X:
Private Sub sEditTxt_TextChanged (Old As String, New As String)
If New.Length>=sTextLen Then
sEditTxt.Text=Old
sEditTxt.SetSelection(sEditTxt.Text.Length,0)
End If
End Sub
The above code doesn't seems to work. Is there any other option to do this? Thanks
Private Sub sEditTxt_TextChanged (Old As String, New As String)
If New.Length > MaxTextLength Then
sEditTxt.Text = New.SubString2(0, MaxTextLength)
sEditTxt.SetSelection(sEditTxt.Text.Length, 0)
End If
End Sub
oparra thank you for your quick response. I think i have used this code before.. but if you run it on iPhone6.. its kind of weird. When i limit the length to 5 character and
the behaviour of the result is as such:
I am able to type 123456... and the moment i type 7, --> it goes back to 12345
Funny. Can anyone clarify this? This is making me go crazy...hmmm..
*This test is using the code above by oparra
* But the code works perfectly fine in B4A
In B4i the text changed event is raised before the text is actually set.
This will work:
B4X:
Private Sub B4XFloatTextField1_TextChanged (Old As String, New As String)
Sleep(100)
Dim MaxLength As Int = 6
Dim t As String = B4XFloatTextField1.Text
If t.Length > MaxLength Then
B4XFloatTextField1.Text = t.SubString2(0, MaxLength)
End If
End Sub