问题
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 :]
回答1:
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.
回答2:
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;
}
回答3:
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;
}
回答4:
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)
}
}
来源:https://stackoverflow.com/questions/17324057/disable-firing-textchanged-event