I have a form with a tab control on it. On each tab page I have lots of checkboxes which represent answers to questions that are presented to the user.
I only want to save what the user has answered and am struggling with a way to iterate through the controls on each tabpage and saving only what the user has answered OR HAS CHANGED, in which I would just update the database instead of "inserting" a record.
Any suggestions on detecting changes?
Thanks in advance...
Loading
DanielPosted Nov 16, 2007, 11:38 AM
Thanks for the great info. I built a protoype yesterday to test different wasy to do it and I will try out your second suggestion.
Here's what I did:
I created a new property to the checkbox control called "HasChanged". On the Load of the form, I iterate all the checboxes and wire the CheckStateChanged event to a method i created. The method I created sets that property to TRUE when triggered. That works and is simple, but does not account for let's say someone, unchecked a checkbox, and then deciding to recheck it, which should NOT be marked as a change. I need a way to account for a "reset" to the original state and not mark that as a change.
I haven't worked with hashtables before, so I'll try to brush up on that concept.
Thanks again.
AlanPosted Nov 15, 2007, 10:08 AM
The only way I can think of doing this without handling the CheckChanged event is to save the initial 'Checked' state of the CheckBoxes to a HashTable or generic Dictionary field. The key would be the Name property of the CheckBox and the value would be a boolean - true (checked) or false (unchecked).
When you're iterating through the CheckBoxes, you can then compare the current state of the Checked property with the initial state. If it's changed, you can save it to the database and (if the operation may be repeated) update the HashTable or Dictionary with the new value.
Alternatively, you could handle the CheckChanged event but just save the CheckBox that has changed, and its new value, to a HashTable or Dictionary. When the Save button is pressed, you could then just iterate through the HashTable and save the changes to the database before clearing the HashTable if the operation is to be repeated.
Using this second approach, if the user changed the same CheckBox twice in the same session, so that it reverted to its original state, you'd need to remove it from the HashTable so that it wouldn't be saved to the database.
DanielPosted Nov 15, 2007, 8:56 AM
I have considered that, but my itention was to process all the checkboxes when the user clicks the save button. Basically when the save is initiated, iterate through all the checkboxes, find the ones that have been changed and save only those to the database. Hope that makes more sense.
Based on that, any additional thoughts???
Jan MontanoPosted Nov 14, 2007, 10:26 PM