B4J Question TextArea & Selection

T201016

Active Member
Licensed User
Longtime User
Hi,
When a user tries to select a word inside a TextArea with a click, in most cases the selection contains the word + a space.
How to select just a word without the accompanying space character. Any ideas?

screen.png
 
Solution
That selection is done with a double click. If it's done with a drag, I would assume that the space was intended to be selected, so something like this maybe:

B4X:
Private Sub TextArea1_MouseClicked (EventData As MouseEvent)
    If EventData.PrimaryButtonPressed And EventData.ClickCount > 1 Then
        If TextArea1.SelectionStart >= TextArea1.SelectionEnd Then Return
        Dim SelectedText As String = TextArea1.Text.SubString2(TextArea1.SelectionStart,TextArea1.SelectionEnd)
        If SelectedText.CharAt(SelectedText.Length - 1) = " " Then
            TextArea1.SetSelection(TextArea1.SelectionStart, TextArea1.SelectionEnd - 1)
        End If
    End If
End Sub

Edit: causes a problem if Control is held (nothing is...

stevel05

Expert
Licensed User
Longtime User
That selection is done with a double click. If it's done with a drag, I would assume that the space was intended to be selected, so something like this maybe:

B4X:
Private Sub TextArea1_MouseClicked (EventData As MouseEvent)
    If EventData.PrimaryButtonPressed And EventData.ClickCount > 1 Then
        If TextArea1.SelectionStart >= TextArea1.SelectionEnd Then Return
        Dim SelectedText As String = TextArea1.Text.SubString2(TextArea1.SelectionStart,TextArea1.SelectionEnd)
        If SelectedText.CharAt(SelectedText.Length - 1) = " " Then
            TextArea1.SetSelection(TextArea1.SelectionStart, TextArea1.SelectionEnd - 1)
        End If
    End If
End Sub

Edit: causes a problem if Control is held (nothing is selected) while double clicking so need to test that the selection contains text and ignore if not.

Or just trim it before using the value.
 
Last edited:
Upvote 0
Solution

T201016

Active Member
Licensed User
Longtime User
That selection is done with a double click. If it's done with a drag, I would assume that the space was intended to be selected, so something like this maybe:

B4X:
Private Sub TextArea1_MouseClicked (EventData As MouseEvent)
    If EventData.PrimaryButtonPressed And EventData.ClickCount > 1 Then
        If TextArea1.SelectionStart >= TextArea1.SelectionEnd Then Return
        Dim SelectedText As String = TextArea1.Text.SubString2(TextArea1.SelectionStart,TextArea1.SelectionEnd)
        If SelectedText.CharAt(SelectedText.Length - 1) = " " Then
            TextArea1.SetSelection(TextArea1.SelectionStart, TextArea1.SelectionEnd - 1)
        End If
    End If
End Sub

Edit: causes a problem if Control is held (nothing is selected) while double clicking so need to test that the selection contains text and ignore if not.

Or just trim it before using the value.

I found my own way using a timer, but I see you have shorter source code, I'll use it. Thanks
 

Attachments

  • Example TextArea (selection words).zip
    4.1 KB · Views: 1
Upvote 0
Top