Edittext maxlength

gobblegob

Member
Licensed User
Longtime User
Hi mate I had a quick look through the documentation and it doesn't mention length as a property, maybe you could try something like string length using the edittext textchanged event

edittext1.text = string
if string.length = "3" then
edittext1.enabled = false
else
next if

or something like that maybe, hope it helps.

cheers Waz
 
Last edited:
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
You will need to handle the TextChanged event. If the text is longer than x characters you should call Substring to truncate it.

B4X:
Sub txtTruckNumber_TextChanged (Old As String, New As String)

If New.Length>19 Then

txtTruckNumber.text= New.SubString2(0,19)

txtTruckNumber.SelectionStart=19

EndIf

End Sub

The selectionStart keeps the cursor at the end of the textbox
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
I re-open this thread because I want to contribute with my solution, for me better than post #4, because it works better with SelectionStart when you change text at the middle of EditText

B4X:
Sub txtTruckNumber_TextChanged (Old As String, New As String)
   If New.Length > 19 Then
       Dim pos As Int = Codice.SelectionStart
       Codice.Text = Old
       Codice.SelectionStart= pos - 1
   End If
End Sub
 
Last edited:
Upvote 0
Top