B4J Question [SOLVED][B4X] TextArea: max. number of characters

GMan

Well-Known Member
Licensed User
Longtime User
Is it possible to restrict a textarea to i.e. the max. of 80 characters, may be a min-length, too ?
 

GMan

Well-Known Member
Licensed User
Longtime User
Sure, i am using it this way - but its not good.

B4X:
Private Sub TextArea1_TextChanged (Old As String, New As String)
    If TextArea1.Text.Length > 80 Then
        TextArea1.Editable = False
    End If
End Sub
Its not good, because if the are more then 80 chars the user cant correct it.

I need a choice to stop the input, so disable the input
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
You should try
:
B4X:
Private Sub TextArea1_TextChanged (Old As String, New As String)
    If new.Length > 80 Then
        TextArea1.text = old
        TextArea1.setSelection(textarea1.text.length,textarea1.text.length) ' put the cursor at the end of the text
    End If
End Sub

or

B4X:
Private Sub TextArea1_TextChanged (Old As String, New As String)
    If new.Length > 80 Then
        TextArea1.text = new.substring2(0,79)
        TextArea1.setSelection(textarea1.text.length,textarea1.text.length) ' put the cursor at the end of the text
    End If
End Sub

This is probably better as it can cope with pasting in lots of text.

edit: minor corrections to if statement
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thx, that does the trick :)
 
Upvote 0
Top