this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
(...)
int test = 0;
private void textBox1_Enter(object sender, EventArgs e)
{
///
/// update completion from db
///
++test;
Log("got focus " + test);
}
I get this result from my log statements:
[03/08/2013 13:56:40]: got focus 1
[03/08/2013 13:56:40]: got focus 2
Why is this function called twice each time I click in my text box?
I already checked: I have only one reference to this function.
Edit:
real function looks more like that
private void textBox1_Enter(object sender, EventArgs e)
{
// update completion
List<string> allValues = getValuesFromDb();
myAutoComplete = new AutoCompleteStringCollection();
myAutoComplete.AddRange(allValues.ToArray());
textBox1.AutoCompleteCustomSource = myAutoComplete; /// this line calls enter event again
++test;
Log("got focus " + test);
}
question solved I know why. calling :
textBox1.AutoCompleteCustomSource = myCustomSource;
call enter event again.
So now how to prevent it ?
1) (not working) first solution: move the following piece of code elsewhere
textBox1.AutoCompleteCustomSource =...
not good: autocompletion is not updated
2) (working) put a lock as
int test = 0;
bool lockEnter = false;
private void textBox1_Enter(object sender, EventArgs e)
{
if (!lockEnter)
{
lockEnter = true;
// update completion
List<string> allValues = getValuesFromDb();
myAutoComplete = new AutoCompleteStringCollection();
mtAutoComplete.AddRange(allValues.ToArray());
textBox1.AutoCompleteCustomSource = myAutoComplete;
++test;
Log("update completion " + test);
lockEnter = false;
}
}
give result excepted.
Thanks you guys !
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