禁用激发TextChanged事件
本文关键字:TextChanged 事件 | 更新日期: 2023-09-27 18:20:10
我有文本框,当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
上,它的工作原理是:]
只需删除事件处理程序,然后在完成所需操作后添加它。
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;
}
我能想到的最简单的方法是使用条件bool
变量。当您要将LostFocus
上的文本设置为true
,并在textChanged
事件处理程序内检查bool
变量是否为true
时,请不要执行任何操作。
我认为您也可以简单地使用;IsFocused";bool内置到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);
}
}
这样,您就不需要为每个输入使用唯一的bool。
HTH
我觉得。。我们可以用最好的方法和简单的方法做到这一点。。!
//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;
}
如果有多个文本框,只需使用字符串列表来存储状态为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)
}
}