views:

511

answers:

3

In Winforms what is proper way to keep a user from changing the value of a DropDown? I want to prompt the user to say that there are unsaved changes. If the user decides to not throw away these changes I want to cancel the combobox changing. Any ideas on how to do this?

I though I had seen a e.Cancel option before. But maybe not on System.Windows.Forms.ComboBox.

UPDATE: To further explain the ComboBox is not part of a data entry form. It is being used like a navigation/filter. User has made changes on a grid. I want to prevent them from navigating away from the grid without getting prompted.

+1  A: 

On combo change event save the old value, give the prompt to the user. If they elect to continue then don't do anything. If they elect to cancel and not lose their changes then set the selected item to the old value.

tster
+1  A: 

You might want to use the combo box's Validating event which is a CancelEventHandler.

MSDN: Control.Validating event

C-Pound Guru
See update. Does the validation event only fire when you try to leave the control?
tyndall
Yeah--it fires when the control loses focus.
C-Pound Guru
+2  A: 

I'm not sure I fully understand your question, but it seems that your question is not really directly related to the ComboBox, but is rather about how to reset a form to its original values when a user opts to not save the changes they have just made.

Presumably, you have a method in your form that loads the controls from values in a data structure of some sort (an ORM object or a DataRow, perhaps). The easiest way to reset your form to its original (pre-edited) values is to simply call that method again with the original (unchanged) data structure.

Update: well, a simple way to do this is to set the ComboBox's Enabled property to false as soon as they change anything on the form, and then set it to true again as soon as they save or cancel their changes.

MusiGenesis
See update to question.
tyndall
+1 that may work.
tyndall