Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with databinding + calculating values as users enter text 1

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
0
0
US
I have an order entry app that divides the data entry between different tabs. I have a square footage and a 50 cent surcharge based on the square footage with both textboxes on the same tab. The salespeople want the surcharge to calculate as they are typing in the footage.

I have tried both of the following:

1) This approach places the result directly into surcharge textbox
Code:
private void txtFootage_TextChanged(object sender, System.EventArgs e) {
txtSurcharge.Text = (double.Parse(txtFootage.Text) * 0.50).ToString();
}
Problem: When you switch tabs, the databinding does not realize that the surcharge textbox has changed and forces the old, stale value back into the surcharge textbox. Therefore, if the surcharge starts out at $50, the footage is changed and the surcharge ends up at $100, it will change back to $50 when you switch tabs. The $100 you place into the textbox never makes it into the DataTable.

2) This approach places the result into the underlying DataTable
Code:
private void txtFootage_TextChanged(object sender, System.EventArgs e) {
tblOrder.Surcharge = double.Parse(txtFootage.Text) * 0.50;
}
Problem: When you update the DataTable in the middle of the edit, it forces the original value of the footage back into the textbox that you are editing. Therefore, if the footage starts at 48 and you delete the 4, the text will be set back to 48 even as you are typing.
 
i think that if you combine the two it should do.
in the change event use the method 1, and in the onExit use code from method two. this way you will see the real time calculation while saving the data when you exit the textfield.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Here are the rules as I have found them:

1) For the source of a calculation, use the control if it is visible, otherwise use the data source. The control may not be populated with data from the data source if it is not visible. Also, the data source may not have the user editing from the control if it is visible.

2) For the target of a calculation, use the control if it is visible, otherwise, use the data source. If the control is not visible, changes made to it never make it to the data source.

Now, if you did not want to recalculate real time as the user made changes, you could always target the data source. However, if you set the data source in the middle of a user edit, the original value from the data source is forced back into the control, blowing away the value that the user typed, EVEN IF YOU SET A DIFFERENT FIELD THAN THE USER WAS EDITING.

3) Finally, you may be thinking, well, what if you have to target a control that is not visible? You would have to set the data source!

For the trigger of recalculating, use the TextChanged, ValueChanged... PropertyChanged event if the TARGET of the calculation is visible, otherwise, use the ColumnChanged event of the data source. Yes, this will not recalculate the target in real time as the user edits the control, however, since the target is not visible, there is no way for the user to know this.

---------------------------------

My specific problem with the above was that I did not call CurrencyManager.EndCurrentEdit as the user switched tabs in my order entry app. For that reason, changes that I made in code to the text of a second texbox were never pulled from the textbox.

Hopefully this will help someone with future problems!
 
DaZZleD,

Thank you for the suggestion! I was going to check my controls for visibility but now I realize I can do the following:

1) Always work with control values during the PropertyChanged events both as sources and targets of calculations.

2) Also catch the DataTable_ColumnChanged event but work with data source sources and targets of calculations there.

This way I am always doing the same thing all the time = more maintainable plus I am not breaking da rulz. It doesn'
t matter if the calculations will not be picked up from the controls because the calculations also occur when the data source is set. Also, it doesn't matter that I am setting the data source because the ColumnChanged event does not occur while the user edits a control, only OnExit as you said.

Also, my calculations will all be consolidated in the DataTable_ColumnChanged event making documentation easier and not half there and half scattered all over in PropertyChanged events.
 
well.. you did a good job!

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top