FreakyAli / Maui.FreakyControls

FreakyControls is a free OSS UI Kit for .NET MAUI which provides a set of controls and utilities to build modern mobile apps.
MIT License
312 stars 37 forks source link

Select all text on TextInputLayout #112

Closed FrankVDL2471 closed 5 months ago

FrankVDL2471 commented 8 months ago

Hello,

I want all the text to be selected, when the control gets focus (is tabbed on) I tried the code below, but it does not seem to work. Any suggestions on how to make this work ?


public class SjwingEntry : FreakyTextInputLayout, IDisposable {

    public SjwingEntry() {
        this.BorderType = Maui.FreakyControls.Shared.Enums.BorderType.Full;
        this.BorderCornerRadius = 5;
        this.BorderStrokeThickness = 2;
        this.BorderStroke = Brush.DarkGray;
        this.ControlBackgroundColor = Colors.White;
        this.TextColor = Colors.Black;
        this.FontFamily = "Default";
        this.FontSize = 16;

        this.Focused += SjwingEntry_Focused;
    }

    public void Dispose() {
        this.Focused -= SjwingEntry_Focused;
    }

    private void SjwingEntry_Focused(object sender, FocusEventArgs e) {
        this.CursorPosition = 0;
        this.SelectionLength = this.Text.Length;
    }
}
FreakyAli commented 8 months ago

I just checked this, seems to be an issue but I am not sure if I can actually add a fix to this soon, To be honest not even sure if I know how to fix this lol.

FreakyAli commented 8 months ago

So after some RnD, I found that as soon as you add the Entry into a ContentView custom control these properties stop working on Focus, But I am not 100% sure why yet!

FreakyAli commented 6 months ago

As of right now, i think this is not fixable but i am still doing some RnD for this hopefully when i find a solution i will update you!

FreakyAli commented 5 months ago

@FrankVDL2471 Need your help, I just tried this with the latest version of Android and this is not working with a basic entry, can you confirm if this works for you there?

FreakyAli commented 5 months ago

@FrankVDL2471

This should solve your issue, I have just tested it let me know if this works so i can close this issue

public class SjwingEntry : FreakyTextInputLayout, IDisposable
{
    private readonly Entry internalEntry;

    public SjwingEntry()
    {
        this.BorderType = Shared.Enums.BorderType.Full;
        this.BorderCornerRadius = 5;
        this.BorderStrokeThickness = 2;
        this.BorderStroke = Brush.DarkGray;
        this.ControlBackgroundColor = Colors.White;
        this.TextColor = Colors.Black;
        this.FontFamily = "Default";
        this.FontSize = 16;
        internalEntry = this.FindByName<Entry>("EntryField");
        internalEntry.Focused += SjwingEntry_Focused;
    }

    protected void Dispose()
    {
        internalEntry.Focused -= SjwingEntry_Focused;
    }

    private async void SjwingEntry_Focused(object sender, FocusEventArgs e)
    {
        internalEntry.SelectionLength = 0;
        await Dispatcher.DispatchAsync(() =>
        {
            internalEntry.CursorPosition = 0;
            internalEntry.SelectionLength = this.Text.Length;
        });
    }
}
FrankVDL2471 commented 5 months ago

Just tested it, and it seems to be working fine.

Thanks for the update

FreakyAli commented 5 months ago

Awesome, I will. be closing this in that case!!