iOS Question Limiting textfield length

stevenindon

Active Member
Licensed User
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
 

TILogistic

Expert
Licensed User
Longtime User
test:
B4X:
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
 
Upvote 0

stevenindon

Active Member
Licensed User
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
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
 
Last edited:
Upvote 0

stevenindon

Active Member
Licensed User
Erel,

Tested the above code.. The funny thing is if i run on debug mode, Everything is fine. But if i run on compile mode, The problem occurs again.

Problem :
"I am able to type 123456... and the moment i type 7, --> it goes back to 12345"

I am running on iphone 6
 
Upvote 0

stevenindon

Active Member
Licensed User
Thank you Erel. I have been playing with the sleep as per your suggestion.. and yes indeed it works. Thank you.

After playing with it, Sleep(5) will do a nice job. Thanks Erel
 
Upvote 0
Top