1

There is a combobox with some items.

If user wants to select the other item, I want to show a Yes-No question box to confirm.

If the user clicks on 'No' , then change will be canceled.

I do this in "SelectionChanged" event handle, but if I cancel the change and select the old item, this event fires again and agin...

  • How to avoid it?
  • How to ask yes/no before changing the selection?
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
TownDrin
  • 69
  • 4
  • check out the [post](https://stackoverflow.com/questions/8608128/how-to-cancel-a-combobox-selectionchanged-event) – neelesh bodgal Jun 17 '20 at 07:14
  • 1
    First thing what comes in mind is to use flag to prevent event processing, see [this answer](https://stackoverflow.com/a/8843675/1997232). If you would use MVVM (or bingings at least), then you can run this logic in the setter of property bound to `SelectedItem`, this way it won't backfire. – Sinatr Jun 17 '20 at 07:25

2 Answers2

0

If you want to ask user after user trying to change that use this event handler SelectionChangeCommitted. You can also change the state of Combo box to initial form by simply set the index number of combobox to -1

 comboBox1.SelectedIndex = -1;

so you can write the code above in click event handler of "No" button.

Hamed Sanaei
  • 121
  • 13
0

If you do not care about displaying the value to change to before really accepting the change (by clicking "Yes") you could try to use the SelectionChangeCommitted event handler to handle these changes and display the user dialog.

vonludi
  • 419
  • 2
  • 20
  • 2
    This is a WPF project ,no SelectionChangedCommitted Event there – TownDrin Jun 17 '20 at 08:30
  • Ahh, you're right, that's only available in Forms. Have you tried (1) disconnecting the `EventHandler`, (2) setting the selection back, and (3) re-attaching the `EventHandler`? – vonludi Jun 18 '20 at 12:28