Tutorial :How to prevent/cancel a combobox's value change in c#?


I have a combobox at the top of a form that loads editable data into fields below. If the user has made changes, but not saved, and tries to select a different option from the combobox, I want to warn them and give them a chance to cancel or save.

I am in need of a "BeforeValueChange" event with a cancelable event argument.

Any advice on how to accomplish?


Save the ComboBox's SelectedIndex when to box if first entered, and then restore it's value when you need to cancel the change.

cbx_Example.Enter += cbx_Example_Enter;  cbx_Example.SelectionChangeCommitted += cbx_Example_SelectionChangeCommitted;    ...    private int prevExampleIndex = 0;  private void cbx_Example_Enter(object sender, EventArgs e)  {      prevExampleIndex = cbx_Example.SelectedIndex;  }    private void cbx_Example_SelectionChangeCommitted(object sender, EventArgs e)  {      // some custom flag to determine Edit mode      if (mode == FormModes.EDIT)       {          cbx_Example.SelectedIndex = prevExampleIndex;      }  }  


Here is the simplest fix:-

        bool isSelectionHandled = true;            void CmbBx_SelectionChanged(object sender, SelectionChangedEventArgs e)          {              if (isSelectionHandled)              {                  MessageBoxResult result = MessageBox.Show("Do you wish to continue selection change?", this.Title, MessageBoxButton.YesNo, MessageBoxImage.Question);                  if (result == MessageBoxResult.No)                  {                      ComboBox combo = (ComboBox)sender;                      isSelectionHandled = false;                      if (e.RemovedItems.Count > 0)                          combo.SelectedItem = e.RemovedItems[0];                      return;                  }              }              isSelectionHandled = true;          }  


Save the current value on the Enter event. Implement the BeforeValueChange logic in the ValueChanged event, before the actual ValueChanged logic. If the user cancels, set the stored value and don't continue in the method (return).

If you're going to use this system a lot, I'd suggest inheriting ComboBox and implementing your BeforeValuechange event there.


The Validating event can be used for this scenario


You don't get an appropriate event by default. You could cache the previous value and set it back to that if the user wants to cancel.


How about using the Validating / Validated events?
It works well, if the event happening on LostFocus instead of Change is ok with you.

Otherwise, how about

public void Combobox_ValueChanged(object sender, EventArgs e) {      if (!AskUserIfHeIsSureHeWantsToChangeTheValue())      {          // Set previous value          return;      }        // perform rest of onChange code  }  


You could use a message filter to intercept clicks and key presses, which would allow you to prevent the combo box's normal behaviour. But I think you'd be better off disabling the combo box when the user makes a change, and require them to either save or revert their changes.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »