SELECTION IN ENTRY

Giorgio Sfiligoi 146 Reputation points
2022-10-12T19:14:11.82+00:00

Entry exposes the property SelectionLenth that returns the number of characters currently selected, but it does not tell their position in the string. May I safely assume that they start from CursorPosition?
When I manually select in my cellular, two cursors are displayed that delimit the start and end of the selection - I am wary about which position is referenced by CursorPosition vs. the current selection.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,391 Reputation points Microsoft Vendor
    2022-10-13T03:00:36.89+00:00

    Hello,

    Entry exposes the property SelectionLenth that returns the number of characters currently selected, but it does not tell their position in the string. May I safely assume that they start from CursorPosition?

    the property SelectionLenth is not provide the position from the entry, you could use the following code to verify it.

       <StackLayout>   
           <Entry x:Name="test_entry" TextColor="Black"/>  
           <Label x:Name="sel" TextColor="Black" BindingContext="{x:Reference Name=test_entry}" Text="{Binding Path=SelectionLength}"/>  
           <Label x:Name="cur" TextColor="Black" BindingContext="{x:Reference Name=test_entry}" Text="{Binding Path=CursorPosition}"/>  
       </StackLayout>  
    

    If you want to get the selected string from Entry.Text, you could use [Substring(Int32, Int32)](https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=net-6.0#system-string-substring(system-int32-system-int32\)) to get it by Text.Substring(CursorPosition, SelectionLength).

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful