Auto-Saving a Tabpanel

I wrote a desktop app with a tabpanel with Save buttons on each of the three panels. My users are asking me to remove the Save buttons and have the data automatically saved to the db. Besides the tabpanel, there are two listboxes (customers and orders) along with multiple radio buttons and check boxes that allow the listboxes to be populated and sorted differently. There is also a menu on this window for all the other features of the program.

I need to call the save function wherever data on the visible panel has changed. Saving when switching panels is easily handled in the TabPanel.Change event. When the user selects a control outside the tab panel, the data also needs to be saved. Is there a way to do this without adding code to each of controls and menu handlers? Since this is a Window app, I would like to use something like TabPanel1.LostFocus, but that doesn’t work as the TabPanel loses focus when on of the controls on it gets focus.

Do you care if the data has actually been changed or not? If not, you could save the record when closing the window, or on a change in selection of one or both of the listboxes. If you do care then you should look at subclassing your controls to fire a HasChanged event or similar.

save when you close the window ?

Good suggestions, but what I was hoping to find was a way to detect if the user had selected anything not on the tab panel. It appears the only way to handle this is to have the first line of the appropriate control action events save to the database.

Subclass all the controls on the tabpanels and have them set a dirty flag (probably on the enclosing window for simplicity) when they change. Have a timer that watches the flag every 200ms or so and writes out the database when it finds they’re dirty.


Dean, why not use the TabPanel’s MouseExit event? That way every time the user moves the mouse out of the TabPanel you can have the app save the data on the current tab. You can, as noted, also save when moving to a different tab.

Using MouseExit is not a good idea. You have a second window or another app moving to the front and MouseExit doesn’t fire consistently.

I save the prefs for each control separately.