[ACCEPTED]-How to prevent/cancel a combobox's value change in c#?-combobox
Save the ComboBox's SelectedIndex when to 2 box if first entered, and then restore it's 1 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;
}
0
Save the current value on the Enter
event.
Implement 6 the BeforeValueChange
logic in the ValueChanged
event, before the actual 5 ValueChanged
logic. If the user cancels, set the stored 4 value and don't continue in the method (return
).
If 3 you're going to use this system a lot, I'd 2 suggest inheriting ComboBox and implementing 1 your BeforeValuechange
event there.
The Validating event can be used for this 1 scenario
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx
You don't get an appropriate event by default. You 2 could cache the previous value and set it 1 back to that if the user wants to cancel.
How about using the Validating / Validated 3 events?
It works well, if the event happening 2 on LostFocus instead of Change is ok with 1 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 6 key presses, which would allow you to prevent 5 the combo box's normal behaviour. But I 4 think you'd be better off disabling the 3 combo box when the user makes a change, and 2 require them to either save or revert their 1 changes.
You can't really prevent it, but you can 2 change it back to the old value if certain 1 requirements aren't met:
private SomeObject = selectedSomeObject=null;
private void cbxTemplates_SelectionChangeCommitted(object sender, EventArgs e)
{
if (!(sender is ComboBox cb)) return;
if (!(cb.SelectedItem is SomeObject tem)) return;
if (MessageBox.Show("You sure?", "??.",
MessageBoxButtons.OKCancel) != DialogResult.OK)
cb.SelectedItem = selectedSomeObject;
else
{
selectedSomeObject = tem;
}
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.