TextChanged fires twice.

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

I'm a bit new to this Windows Forms thing and a bit lost on this one.
This app I'm on has a TextCell class (inheriting from something called
TableCell, which in turn inherits from Panel) with a TextBox. This
TextBox is bound to a table in a DataSet. So here's the problem: when
the app loads if I change a text field (TextBox in a TextCell with text
of say "monkey"), then move on to another, then put the edited one back
in focus, then change it (say hit the 's' key)... TextChanged then
fires twice, once for the change ("monkey" + "s"), then another time to
change it back to the previously edited but pre-re-focused value
("monkey"). So for a split second the field says "monkeys" then
immediately changes back to "monkey" and puts the cursor at the
beginning of the text. Make sense? Subsequent changes work fine, so
long as it never loses focus.

Sample:

public TextCell(...) {

this._textBox.DataBindings.Add("Text",AppStart.Instance.dsDataSubSet.Tables[this._tableName],"data");
this._textBox.TextChanged += new EventHandler(_textBox_TextChanged);
}

private void _textBox_TextChanged(object sender, EventArgs e) {
DataRow[] dr =
AppStart.Instance.dsData.Tables[0].Select(...stuff...);
if(dr.Length == 0){
...
NewRow = a new row
...
AppStart.Instance.dsData.Tables[0].Rows.Add(NewRow);
}
else{
dr[0]["data"] = this._textBox.Text;
}

this._textBox.BindingContext[AppStart.Instance.dsDataSubSet.Tables[this._tableName]].EndCurrentEdit();
}

I've seen that there may be a bug if the Text property is changed
programmatically, but that's not the case here. Ideas anyone?

Thanks,
Eric
 
Eric said:
I'm a bit new to this Windows Forms thing and a bit lost on this one.
This app I'm on has a TextCell class (inheriting from something called
TableCell, which in turn inherits from Panel) with a TextBox. This
TextBox is bound to a table in a DataSet. So here's the problem: when
the app loads if I change a text field (TextBox in a TextCell with
text of say "monkey"), then move on to another, then put the edited
one back in focus, then change it (say hit the 's' key)...
TextChanged then fires twice, once for the change ("monkey" + "s"),
then another time to change it back to the previously edited but
pre-re-focused value ("monkey"). So for a split second the field
says "monkeys" then immediately changes back to "monkey" and puts the
cursor at the beginning of the text. Make sense? Subsequent changes
work fine, so long as it never loses focus.

Sample:

public TextCell(...) {

this._textBox.DataBindings.Add("Text",AppStart.Instance.dsDataSubSet.T
ables[this._tableName],"data"); this._textBox.TextChanged += new
EventHandler(_textBox_TextChanged); }

private void _textBox_TextChanged(object sender, EventArgs e) {
DataRow[] dr =
AppStart.Instance.dsData.Tables[0].Select(...stuff...);
if(dr.Length == 0){
...
NewRow = a new row
...
AppStart.Instance.dsData.Tables[0].Rows.Add(NewRow);
}
else{
dr[0]["data"] = this._textBox.Text;
}

this._textBox.BindingContext[AppStart.Instance.dsDataSubSet.Tables[thi
s._tableName]].EndCurrentEdit(); }

I've seen that there may be a bug if the Text property is changed
programmatically, but that's not the case here. Ideas anyone?

Root cause of these things is often the fact that the event handler is
bound twice to the event. Please check at runtime how many times you
bind the eventhandler to the event.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
I have also seen this kind of issue when a secondary property fires it's
FooChanged() event. I'm not sure if this is possible with Datasets and calculated
columns as I typically avoid dataset binding, but YMMV.
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Eric said:
I'm a bit new to this Windows Forms thing and a bit lost on this one.
This app I'm on has a TextCell class (inheriting from something
called
TableCell, which in turn inherits from Panel) with a TextBox. This
TextBox is bound to a table in a DataSet. So here's the problem:
when
the app loads if I change a text field (TextBox in a TextCell with
text of say "monkey"), then move on to another, then put the edited
one back in focus, then change it (say hit the 's' key)...
TextChanged then fires twice, once for the change ("monkey" + "s"),
then another time to change it back to the previously edited but
pre-re-focused value ("monkey"). So for a split second the field
says "monkeys" then immediately changes back to "monkey" and puts the
cursor at the beginning of the text. Make sense? Subsequent changes
work fine, so long as it never loses focus.
Sample:

public TextCell(...) {

this._textBox.DataBindings.Add("Text",AppStart.Instance.dsDataSubSet.
T ables[this._tableName],"data"); this._textBox.TextChanged += new
EventHandler(_textBox_TextChanged); }

private void _textBox_TextChanged(object sender, EventArgs e) {
DataRow[] dr =
AppStart.Instance.dsData.Tables[0].Select(...stuff...);
if(dr.Length == 0){
...
NewRow = a new row
...
AppStart.Instance.dsData.Tables[0].Rows.Add(NewRow);
}
else{
dr[0]["data"] = this._textBox.Text;
}
this._textBox.BindingContext[AppStart.Instance.dsDataSubSet.Tables[th
i s._tableName]].EndCurrentEdit(); }

I've seen that there may be a bug if the Text property is changed
programmatically, but that's not the case here. Ideas anyone?
Root cause of these things is often the fact that the event handler
is bound twice to the event. Please check at runtime how many times
you bind the eventhandler to the event.

FB
 
Frans said:
Eric said:
I'm a bit new to this Windows Forms thing and a bit lost on this one.
This app I'm on has a TextCell class (inheriting from something called
TableCell, which in turn inherits from Panel) with a TextBox. This
TextBox is bound to a table in a DataSet. So here's the problem: when
the app loads if I change a text field (TextBox in a TextCell with
text of say "monkey"), then move on to another, then put the edited
one back in focus, then change it (say hit the 's' key)...
TextChanged then fires twice, once for the change ("monkey" + "s"),
then another time to change it back to the previously edited but
pre-re-focused value ("monkey"). So for a split second the field
says "monkeys" then immediately changes back to "monkey" and puts the
cursor at the beginning of the text. Make sense? Subsequent changes
work fine, so long as it never loses focus.

Sample:

public TextCell(...) {

this._textBox.DataBindings.Add("Text",AppStart.Instance.dsDataSubSet.T
ables[this._tableName],"data"); this._textBox.TextChanged += new
EventHandler(_textBox_TextChanged); }

private void _textBox_TextChanged(object sender, EventArgs e) {
DataRow[] dr =
AppStart.Instance.dsData.Tables[0].Select(...stuff...);
if(dr.Length == 0){
...
NewRow = a new row
...
AppStart.Instance.dsData.Tables[0].Rows.Add(NewRow);
}
else{
dr[0]["data"] = this._textBox.Text;
}

this._textBox.BindingContext[AppStart.Instance.dsDataSubSet.Tables[thi
s._tableName]].EndCurrentEdit(); }

I've seen that there may be a bug if the Text property is changed
programmatically, but that's not the case here. Ideas anyone?

Root cause of these things is often the fact that the event handler is
bound twice to the event. Please check at runtime how many times you
bind the eventhandler to the event.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

FB,

Thanks, I checked that and it registers only once. Since I'm not too
familiar yet with databinding in .net I tried the blindfolded shot in
the dark method at that. Moving the EndCurrentEdit method to the Leave
event (as opposed to within TextChanged) seems to have solved
TextChanged getting caught twice. [shrug] Looks like I have some
reading to do. ha.

Cheers,
Eric
 
Back
Top