Hello,
This example has been cut from a larger context, therefore it looks very
simple. It has just been created to illustrate the error. I have overridden
the standard TextBox so it now looks like this:
public class MyTextBox : System.Windows.Forms.TextBox
{
protected string objectValue;
public string ObjectValue
{
get { return objectValue; }
set
{
this.Text = objectValue as string;
objectValue = value;
}
}
}
I have then created a form and placed several instances of the MyTextBox
control on it. All the controls bind to a property of a simple class.
---
Employee employee = new Employee("Heinz", "Svendsen", 36);
employeeBindingSource.DataSource = employee;
---
this.firstNameTextBox.DataBindings.Add(new
System.Windows.Forms.Binding("ObjectValue", this.employeeBindingSource,
"FirstName", true));
----
Now if I bind the properties of the Employee class to the Text property of
the MyTextBox everything works fine. But if I bind to the "ObjectValue"
property the binding system writes the value from the MyTextBox back to the
employee class everytime I change control. It thinks that the value has ben
changed even though I have changed nothing.
If I call EndEdit() on the bindingSource all controls write back the value
to the Employee class. This also only happens if I databind to the
ObjectValue property. Everything is ok if I use the Text property.
Any ideas?
Best regards
Henrik Skak Pedersen
Linda Liu[MSFT] - 06 Dec 2007 05:17 GMT
Hi Henrik,
I performed a test on your sample code and did see the problem you
described.
The reason why this problem doesn't exist when you bind the Text property
of the MyTextBox to the data source is that there's a TextChanged event in
the TextBox class, which data binding uses to get notified when the Text
property is changed.
Because there's no event that data binding can use to get notified when the
ObjectValue property is changed, data binding forces to push the value of
the ObjectValue property back to the data source every time the MyTextBox
loses the focus. If you add a ObjectValueChanged event in the MyTextBox
class, the problem should be solved. The following is a sample:
public class TextBox1 : System.Windows.Forms.TextBox
{
protected string objectValue;
public string ObjectValue
{
get { return this.objectValue; }
set
{
if (objectValue != value)
{
objectValue = value;
OnObjectValueChanged();
}
}
}
public event EventHandler ObjectValueChanged;
private void OnObjectValueChanged()
{
if (ObjectValueChanged != null)
{
ObjectValueChanged(this, new EventArgs());
}
}
}
Please try the above code in your project to see if the problem is solved
and let me know the result.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Henrik Skak Pedersen - 10 Dec 2007 19:37 GMT
Hi Linda,
Thank you very much for your reply. I had to change the suggested code a bit
because I am using a standard textbox, and the text being entered is being
set in the Text property. But otherwise it is working great. Thank you very
much for your help.
Best regards
Henrik Skak Pedersen
public class TextBox : System.Windows.Forms.TextBox
{
protected string objectValue;
public string ObjectValue
{
get { return objectValue; }
set
{
objectValue = value;
base.Text = objectValue as string;
}
}
public event EventHandler ObjectValueChanged;
private void OnObjectValueChanged()
{
if (ObjectValueChanged != null)
ObjectValueChanged(this, new EventArgs());
}
protected override void OnTextChanged(EventArgs e)
{
this.objectValue = this.Text;
base.OnTextChanged(e);
this.OnObjectValueChanged();
Console.WriteLine(this.Text);
}
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
}