I would like that when the user is typing/changing the text of the textBox1, simultaneously the text of a second textBox2 would get cleared. For doing this, I simply added an event to the form:
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox2.Text = "";
}
However, this is causing the textBox2 getting cleared but the input that the user typed gets lost. Practically:
what I expect:
if the textBox1 text is empty and the textBox2 is not, when the user types "A" into the first text box I would simultaneously get the textBox2 cleared and the letter "A" into the textBox1.
what I get:
the textBox2 gets clear, but the letter "A" does not appear into the textBox1: I will have to type it a second time to get it into the right place.
What should I do to keep the user input into the textBox1 while clearing the textBox2?
EDIT: actually forgetting to add an important part of the code, which is the "twin" brother of the method I've posted above:
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
I slightly revise my question: how can I meet my expected behavior while avoiding that the clearing in textBox2 is taken as a text_changed event?
I would recommend keeping your handlers nice and clean, as you have them, and instead doing one of two things:
Modified property to check if the user has actually modified the textbox. This bool automatically gets reverted to false when programmatic changes to Text are madeModifiedHere's how you could implement the former option:
public void TextBox1_TextChanged(object sender, EventArgs ea)
{
if (textBox1.Modified) textBox2.Clear();
}
This works beautifully and is easy to understand for anyone looking at the code. If the Text property has been changed programmatically (as in your case, by the other event handler), the Modified property is false, and no clearing happens.
As a bonus, if you make programmatic changes elsewhere and do want the event handler to follow through, you can easily set Modified = true explicitly:
textBox1.Text = @"I have been set programmatically,
but in this specific case the other box
should still be cleared";
textBox1.Modified = true;
Here's how you could implement this using keyboard events:
private void TextBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Clear();
}
And where you're doing your event handler to event binding:
textBox1.KeyDown += TextBox1_TextChanged;
textBox1.Paste += TextBox1_TextChanged;
You could make this a bit more generic by adding a method that binds textbox clearing logic to any other textbox. Here's how I used this in my test winforms project:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ClearOnUserInput(textBox1, textBox2);
ClearOnUserInput(textBox2, textBox1);
}
private void ClearOnUserInput(TextBox inputBox, TextBox target)
{
inputBox.TextChanged += delegate {
if(inputBox.Modified) target.Clear();
};
}
}
Note that I've added a separate method to attach the clearing logic to keep things DRY, and I'm also using an anonymous delegate here to avoid adding extra methods. You can just as easily use two handlers as you were earlier.
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