LLBLGen Pro Version: 2.5 Final, December 5, 2007
Runtime Library Version: 2.5.7.1214
Code Generation: C#, .NET 2.0, SelfServicing
Database: SQL Server 2005
The crux of my problem is that I am unable to change the value of a data-bound control from within an event handler of another data-bound control. I have a WinForms form on which the TextBox controls are bound to an EntityCollection<T>-derived class object via a BindingSource object. When the EntityCollection is populated from the database, data from the first Entity in the collection is displayed in the appropriate TextBox controls, as expected. The problem I have occurs when a user is editing that data within the TextBoxes. One of my requirements is that I need to change the value of one of the bound TextBoxes based on the data that is entered in other bound TextBoxes. I'm trying to accomplish this task by setting the value of the target TextBox within the Focus-Leave event of the source TextBox, as follows.
In this example, which is actual code with just the variable names changed, the following types hold:
TextBox txtBlueCount
TextBox txtGreenCount
TextBox txtRedCount
BindingSource bindingSourceColor
Where bindingSourceColor is bound to a ColorCollection : EntityCollectionBase<ColorEntity> object.
private void txtBlueCount_Leave(object sender, EventArgs e)
{
SetGreenCount();
}
private void SetGreenCount()
{
int greenCount = 0;
if (txtRedCount.Text.Length > 0)
{
int redCount = 0;
if (int.TryParse(txtRedCount.Text, out redCount))
greenCount += redCount;
}
if (txtBlueCount.Text.Length > 0)
{
int blueCount = 0;
if (int.TryParse(txtBlueCount.Text, out blueCount))
greenCount -= blueCount;
}
txtGreenCount.Text = greenCount.ToString();
}
When this code executes, txtGreenCount.Text is initially set to the proper value when the focus leaves txtBlueCount, but then it is reset to its original value before control returns to the user. By handling the TextChanged event for txtGreenCount, I can tell that something in this line of code in the setter for the ColorEntity.BlueCount property is resetting the value of ColorEntity.GreenCount:
public virtual Nullable<System.Int32> BlueCount
{
get { return (Nullable<System.Int32>)GetValue((int)ColorFieldIndex.BlueCount, false); }
set { SetValue((int)ColorFieldIndex.BlueCount, value, true); }
}
If I change the last line in the SetGreenCount() method above to the following, then I actually have the opposite problem when leaving the focus of txtBlueCount: txtGreenCount keeps its new value but txtBlueCount gets reset to its original value.
((ColorEntity)bindingSourceColor.Current).GreenCount = greenCount;
I'd appreciate any help or explanations anyone can provide.
Bret Fynewever