7

我有文本框,当被触发时我正在更改其中的文本,lostFocus但这也会触发textChanged我正在处理的事件,但我不希望它在这种情况下被触发,我怎样才能在这里禁用它?

更新:

这个想法bool很好,但我有几个文本框,我对它们都使用相同的事件,所以它不能完全按照我想要的方式工作。

现在它正在工作!:

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;
   }

我在编辑文本后设法将其bool重新打开true,它可以正常工作,谢谢大家:]

4

4 回答 4

12

只需删除事件处理程序,然后在完成所需的操作后添加它。

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;    
}
于 2013-06-26T16:04:26.137 回答
9

我能想到的最简单的方法是使用条件bool变量。当您要设置文本时,LostFocus将其设置为true并在textChanged事件处理程序内部检查该bool变量是否为true,不要什么都不做。

于 2013-06-26T15:20:20.577 回答
1

我觉得..我们可以以最好的方式和简单的方式做到这一点..!

//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;
        }
于 2015-07-16T20:15:57.430 回答
0

如果您有多个文本框,只需使用一个字符串列表来存储状态为DoNotFire.

您更新的代码(还改进了其他内容):

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)
       }
   }
于 2013-06-26T15:54:28.467 回答