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?
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 :]
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.
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.
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;
}
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With