My C# application has a comboBox
with a SelectedIndexChanged
event. Usually, I want this event to fire, but but sometimes I need the event to not fire. My comboBox
is an MRU file list. If a file in the list is found to not exist, the item is removed from the comboBox
, and the comboBox
SelectedIndex
is set to zero. However, setting the comboBox
SelectedIndex
to zero causes the SelectedIndexChanged
event to fire, which in this case is problematic because it causes some UIF code to be run in the event handler. Is there a graceful way to disable/enable events for C# form controls? Thanks.
Start the eventhandler method with
ComboBox combo = sender as ComboBox;
if (combo.SelectedIndex == 0)
{
return;
}
If you're issue is with a different eventhandler you could remove the eventhandler's event registration first.
combo.SelectedIndexChanged -= EventHandler<SelectedIndexChangedEventArgs> SomeEventHandler;
combo.SelectedIndex = 0;
combo.SelectedIndexChanged += EventHandler<SelectedIndexChangedEventArgs> SomeEventHandler;