Domanda

With the prevelence of the "OK" and "Cancel" buttons at the bottom of forms/dialogs, it is odd to me that I can't seem to find a "standard" way to save control state.

For instance, I have a checked list box of filters. When the user clicks the OK button, it applies the filters to a data set and the form closes. If the user clicks the cancel button, the form undoes all the checked-item changes and the form closes.

In a perfect world, when the user clicks the "OK" button, the saved control state is overwritten with the current control state and a new-state flag is set. When the form is closing, if the new-state flag is set the form resets the flag, and if it is not set the form replaces the displayed control with the saved control state. That way if the cancel button is hit, all the checked-changes the user made are reset.

What is the best-practice way of handling a cancel button undoing changes to a control, or even an entire form? Is there a best practice solution? I could see this being necessary for text boxes, radial buttons, check boxes, and practically every control, so please try and keep it generic and not specific to checked list boxes.

È stato utile?

Soluzione

I would suggest it's as simple as:

  • Keep the data reflected in the UI separate from the UI itself
  • When the form is loaded, set its contents based on the data
  • When the user clicks OK, save the changes to the data model (however that is achieved, which will depend on exactly how you're populating the model)
  • When the user clicks Cancel, don't save any changes

There's no need to "undo" the changes on Cancel - you just throw away the form. When you next want to show the form, the same data will be loaded as before, because you didn't save any changes to it.

Altri suggerimenti

The easy way: don't re-use form instances. Do var childForm = new MyChildForm(); before each childForm.Show();

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top