Disable firing TextChanged event
Asked Answered
A

5

10

I have textbox and I'm changing the text inside it when lostFocus is fired but that also fires up the textChanged event, which I'm handling but I don't want it to be fired in this one case, how can I disable it here?

UPDATE:

The idea with bool is good but I have couple of textboxes and I use the same event for all of them, so it's not working exactly as I want the way I want.

Now it's working! :

private bool setFire = true;

private void mytextbox_LostFocus(object sender, RoutedEventArgs e)
   {
      if (this.IsLoaded)
      { 
          System.Windows.Controls.TextBox textbox = sender as System.Windows.Controls.TextBox;
                    
          if(textbox.Text.ToString().Contains('.'))
          {
             textbox.Foreground = new SolidColorBrush(Colors.Gray);
             textbox.Background = new SolidColorBrush(Colors.White);

             setFire = false;
             textbox.Text = "something else";
             setFire = true;
          }
                    
      }
   }
    
private void mytextbox_TextChanged(object sender, TextChangedEventArgs e)
   {
      if ((this.IsLoaded) && setFire)
      {
         System.Windows.Controls.TextBox textbox = sender as System.Windows.Controls.TextBox;
                    
         if(textbox.Text.ToString().Contains('.'))
         {
            textbox.Foreground = new SolidColorBrush(Colors.White);
            textbox.Background = new SolidColorBrush(Colors.Red);
         }  
       }
       
       setFire = true;
   }

I managed to put the bool back on true after editing the text and it works so thx guys :]

Amphitrite answered 26/6, 2013 at 15:17 Comment(3)
then modify @Tigran idea private Dictionary<TextBox, bool> setFire = ...Monocular
No need to have a Dictionary IMHO. A list of String is sufficient as shown in my example.Jollification
Do not post the solution as part of the question! If someone answered it, simple "accept" is enough. If you didn't get a satisfactory answer, but came up with your own solution, post it as the answer.Brakeman
P
9

Simpliest way that I can think of is using conditnional bool variable. When you are going to set the text on LostFocus set it to true and inside textChanged event handler check if that bool variable is true, do not do nothing.

Pergolesi answered 26/6, 2013 at 15:20 Comment(0)
R
13

Just remove the event handler and then add it after you've done what you need to.

private void mytextbox_LostFocus(object sender, RoutedEventArgs e)
{
  this.mytextbox.TextChanged -= this.myTextBox_TextChanged;

  if(textbox.Text.ToString().Contains('.'))
  {
         textbox.Foreground = new SolidColorBrush(Colors.Gray);
         textbox.Background = new SolidColorBrush(Colors.White);
  }

  this.mytextbox.TextChanged += this.myTextBox_TextChanged;    
}
Rioux answered 26/6, 2013 at 16:4 Comment(0)
P
9

Simpliest way that I can think of is using conditnional bool variable. When you are going to set the text on LostFocus set it to true and inside textChanged event handler check if that bool variable is true, do not do nothing.

Pergolesi answered 26/6, 2013 at 15:20 Comment(0)
S
3

I think you could also simply use the "IsFocused" bool built into the UI.

private void mytextbox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textbox = sender as TextBox;
    if (!textbox.IsLoaded || !textbox.IsFocused)
        return;
     
                
    if(textbox.Text.ToString().Contains('.'))
    {
        textbox.Foreground = new SolidColorBrush(Colors.White);
        textbox.Background = new SolidColorBrush(Colors.Red);
    }  
}

This way you don't need a unique bool for each input.

HTH

Shayshaya answered 27/10, 2022 at 0:48 Comment(0)
J
1

I feel.. We can do this in Best way and easy way..!

//My textbox value will be set from other methods
txtNetPayAmount.Text = "Ravindra.pc"; //I wanted to avoide this to travel in my complex logic in TextChanged

private void txtNetPayAmount_TextChanged(object sender, EventArgs e)
        {
            _strLoanPayment = Convert.ToString(txtNetPayAmount.Text).Trim();
            if (string.IsNullOrEmpty(_strLoanPayment)) return;
            if(!_isPayAmountEntered) return;

            //Some logic.. Avoided to run each time on this text change
        }

        private bool _isPayAmountEntered = false;
        private void txtNetPayAmount_Enter(object sender, EventArgs e)
        {
            _isPayAmountEntered = true;
        }

        private void txtNetPayAmount_Leave(object sender, EventArgs e)
        {
            _isPayAmountEntered = false;
        }

        private void txtNetPayAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            _isPayAmountEntered = false;
        }
Jackfish answered 16/7, 2015 at 20:15 Comment(0)
J
0

If you have multiple Textboxes, just use a List of String where you store the textboxes whose state is DoNotFire.

Your updated code (Also improved other things):

private List<string> doNotFireTextBoxes = new List<string>();

private void mytextbox_LostFocus(object sender, RoutedEventArgs e)
   {
      if (this.IsLoaded)
      {      
          System.Windows.Controls.TextBox textbox = (System.Windows.Controls.TextBox) sender;
          doNotFireTextBoxes.Add(textbox.Name)  

          if(textbox.Text.Contains('.'))
          {
             textbox.Foreground = new SolidColorBrush(Colors.Gray);
             textbox.Background = new SolidColorBrush(Colors.White);
          }

      }
   }

private void mytextbox_TextChanged(object sender, TextChangedEventArgs e)
   {
      if (this.IsLoaded)
      {
         System.Windows.Controls.TextBox textbox = (System.Windows.Controls.TextBox) sender;
         if(!doNotFireTextBoxes.Contains(textbox.Name))
         {
             if(textbox.Text.Contains('.'))
             {
                textbox.Foreground = new SolidColorBrush(Colors.White);
                textbox.Background = new SolidColorBrush(Colors.Red);
             }
         }
         doNotFireTextBoxes.Remove(txtBoxName)
       }
   }
Jollification answered 26/6, 2013 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.