Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable firing TextChanged event

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 :]

like image 409
Ms. Nobody Avatar asked Jun 26 '13 15:06

Ms. Nobody


People also ask

How to disable TextChanged event in c#?

Just remove the event handler and then add it after you've done what you need to. Save this answer. Show activity on this post. Simpliest way that I can think of is using conditnional bool variable.

What is TextChanged event in C#?

The TextChanged event is raised when the content of the text box changes between posts to the server. The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.


3 Answers

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;    
}
like image 78
JtM Avatar answered Oct 20 '22 07:10

JtM


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.

like image 38
Tigran Avatar answered Oct 20 '22 07:10

Tigran


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;
        }
like image 20
user1812146 Avatar answered Oct 20 '22 05:10

user1812146