S stevenindon Active Member Licensed User Dec 19, 2021 #1 Hello all, I have been trying to limit text input in the TextField but failed. And here are my codes : B4X: Copy 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
Hello all, I have been trying to limit text input in the TextField but failed. And here are my codes : B4X: Copy 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 Dec 19, 2021 #2 test: B4X: Copy 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
test: B4X: Copy 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
S stevenindon Active Member Licensed User Dec 19, 2021 #3 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: Dec 19, 2021 Upvote 0
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
Erel B4X founder Staff member Licensed User Longtime User Dec 19, 2021 #4 In B4i the text changed event is raised before the text is actually set. This will work: Code: Copy 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: Dec 20, 2021 Upvote 0
In B4i the text changed event is raised before the text is actually set. This will work: Code: Copy 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
S stevenindon Active Member Licensed User Dec 19, 2021 #5 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
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
Erel B4X founder Staff member Licensed User Longtime User Dec 20, 2021 #6 You are correct. A bit hacky but setting the sleep duration to 100 will work. Upvote 0
S stevenindon Active Member Licensed User Dec 20, 2021 #7 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
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