locked
Xamarin.Forms : Entry field textChanged event. RRS feed

  • Question

  • User114914 posted

    Hi,

    In xamarin form Entry control , TextChanged event Entry1 _TextChanged(object sender, TextChangedEventArgs e) , we have "OldTextValue" & "NewTextValue" in event args.

    based on some condition i have to replace new value with old value for that i have used below code,

    e.OldTextValue = e.NewTextValue;

    but this end in multiple or some times infinite loop, as each time old & new values are interchanged.

    is there any way handle the event as true as we do in wpf.

    Thanks,

    Friday, June 19, 2015 5:25 AM

Answers

  • User19820 posted

    oh that makes sense, now think about it. it makes sense it goes into an infinite loop. you're changing the text in a text changed event handler. basically, you're in the text changed event handler, you change text which fires another event handler, and then you set text again, and so on .... you just need a way to change text ONLY when it's needed

    Entry1 _TextChanged(object sender, TextChangedEventArgs e)
    {
        if(.... put some condition here....)
        {
            entryField.Text = e.OldTextValue;
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 19, 2015 7:17 AM

All replies

  • User19820 posted

    e is the argument passed to your event handler. the code you pasted e.OldTextValue = e.NewTextValue; is invalid

    it's not possible to set e.OldTextValue value

    Friday, June 19, 2015 5:47 AM
  • User114914 posted

    Hey thanks for your reply its my mistake , wrongly typed here , it should be

    entryField.Text = e.OldTextValue;

    this triggers event to fire multiple times.

    Friday, June 19, 2015 6:48 AM
  • User62190 posted

    I don't really like it, and there must be another way to handle this, but the fastest way to fix this would be to unsubscribe the event before changing the text inside your event handler and then resubscribe. Something like this:

    Entry1 _TextChanged(object sender, TextChangedEventArgs e)
    {
        if(Invalid)
        {
            entryField.TextChanged -= Entry1 _TextChanged;
            entryField.Text = e.OldTextValue;
            entryField.TextChanged += Entry1 _TextChanged;
        }
    }
    

    I'm pretty sure there is also a way to handle this via bindings.

    Friday, June 19, 2015 7:09 AM
  • User19820 posted

    oh that makes sense, now think about it. it makes sense it goes into an infinite loop. you're changing the text in a text changed event handler. basically, you're in the text changed event handler, you change text which fires another event handler, and then you set text again, and so on .... you just need a way to change text ONLY when it's needed

    Entry1 _TextChanged(object sender, TextChangedEventArgs e)
    {
        if(.... put some condition here....)
        {
            entryField.Text = e.OldTextValue;
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 19, 2015 7:17 AM
  • User270940 posted

    ~~I know this is 2 years ago, but would a better indirect solution be to disable the entry?~~

    Edit: nevermind i realise why that wouldn't be a good solution

    Tuesday, September 19, 2017 12:28 AM