data-bound control issues - setting control values

Posts   
 
    
BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 20-Dec-2007 04:15:17   

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

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Dec-2007 09:07:39   

Instead of changeing the values of the bound controls (textBoxes), try to change the values of the bounded fields of the current Entity.

BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 20-Dec-2007 15:25:52   

At the end of my first post, I mention trying that--if I understand you correctly. I replaced the assignment to the Text property of the control with this code:

((ColorEntity)bindingSourceColor.Current).GreenCount = greenCount;

In this case, instead of the value of my target control getting reset, the value of the source control is reset to what it was before the user changes it and leaves the field.

Interestingly, either version of the code works as long as you don't change the value of the source control. It seems like somewhere down inside the entity code, when a field value is set the values of some other fields in the entity are being reset.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Dec-2007 16:26:19   

I guess because you are binding to an entityCollection, then Edit cycles are used in databinding, and what you are facing is that you start an edit cycle before you end another one that's why the un-ended edit cycle loses the new value.

So what you have to End the edit cycle before starting the new one

More info can be found here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7526 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8818 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8381 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=9392

BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 20-Dec-2007 18:39:45   

I don't think edit cycles have anything to do with this one. There is a single Entity in the Collection, and I'm editing fields within the same Entity.

But no matter--a colleague suggested I try handling the TextBox.Validated event instead of the TextBox.Leave event, and that worked to fix the issue. I made no code changes other than moving the calls to my "SetGreenCount()" method to the Validated handlers rather than the Leave handlers.

Thanks for your help.

Bret

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Dec-2007 11:02:42   

Thanks for the feedback.