title: Xamarin entry control TextChanged event looping round
body: <p>On my form I have 3 <code>entry</code> controls. I'm trying to validate the 'Age' control, with the following validation rules:</p>
  
  <ul>
  <li><p>Cannot enter more than 3 digits</p></li>
  <li><p>Cannot enter a decimal place (.)</p></li>
  <li><p>Cannot enter a hyphen (-)</p></li>
  </ul>
  
  <p>To do this, I've set the 'TextChanged' property of my control to be </p>
  
  <pre><code>TextChanged="OnAgeTextChanged"
  </code></pre>
  
  <p>My <code>OnAgeTextChanged</code> method is:</p>
  
  <pre><code> private void OnAgeTextChanged(object sender, TextChangedEventArgs e)
      {
         var entry = (Entry)sender;
  
          try
          {
  
             if (entry.Text.Length &gt; 3)
              {
                 string entryText = entry.Text;
  
                 entry.TextChanged -= OnAgeTextChanged;
  
                  entry.Text = e.OldTextValue;
                  entry.TextChanged += OnAgeTextChanged;
              }
  
              string strName = entry.Text;
  
              if (strName.Contains(".") || strName.Contains("-"))
              {
                  strName = strName.Replace(".", "").Replace("-", "");
                  entry.Text = strName;
              }
          }
  
          catch(Exception ex)
          {
              Console.WriteLine("Exception caught: {0}", ex);
          }
  
      }
  </code></pre>
  
  <p>However, when the if conditions are met, the event is being looped over multiple times, causing the application to run slowly.</p>
  
  <p>For example, if I enter my age as 1234, it loops over the code multiple times so there's a delay, with the delay increasing each time the text gets changed. </p>
  
  <p>What other way can I achieve this validation, but without the event being called multiple times?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After updating the code to remove the <code>TextChanged</code> trigger on my control before re-assigning it at the end of the method, it still loops over multiple times, and the number of loops increases with each key press. </p>
  
  <p>Entry control <code>xaml</code></p>
  
  <pre><code>&lt;Entry x:Name="txtAge"
         Placeholder="Age"
         Keyboard="Numeric"
         TextColor="DarkBlue"
         PlaceholderColor="DarkBlue"
         Completed="AgeCompleted"
         HorizontalOptions="Start"
         WidthRequest="55"
         TextChanged="OnAgeTextChanged"
  /&gt;
  </code></pre>
  
  <p><code>TextChanged</code> event</p>
  
  <pre><code> private void OnAgeTextChanged(object sender, TextChangedEventArgs e)
      {
  
          var entry = (Entry)sender;
  
          try
          {
  
              entry.TextChanged -= OnAgeTextChanged;
  
              if (entry.Text.Length &gt; 3)
              {
  
                  entry.Text = e.OldTextValue;
              }
  
              string strName = entry.Text;
  
              if (strName.Contains(".") || strName.Contains("-"))
              {
                  strName = strName.Replace(".", "").Replace("-", "");
                  entry.Text = strName;
              }
          }
  
          catch(Exception ex)
          {
              Console.WriteLine("Exception caught: {0}", ex);
          }
  
          finally
          {
  
              entry.TextChanged += OnAgeTextChanged;
          }
       }
  </code></pre>