How can I disable an event handler temporarily in WinForms?
2
-
Can you share what you're doing at a higher level? Because you probably don't want to disable ALL event handlers (the form's default paint and move handlers come to mind). – Joel Coehoorn Apr 13 '09 at 16:38
-
I just don't want to catch changes in the state of a control when updating it programmatically. And yes, I don't want to disable ALL event handlers, just "an event handler". stackoverflow.com/questions/744436/… – George Apr 13 '09 at 16:53
Add a comment
|
Probably, the simplest way (which doesn't need unsubscribing or other stuff) is to declare a boolean value and check it at the beginning of the handler:
bool dontRunHandler;
void Handler(object sender, EventArgs e) {
if (dontRunHandler) return;
// handler body...
}
-
9This is simple, but code will be a mess if you use this often. – Sameera Kumarasingha Dec 3 '15 at 4:26
-
Disable from what perspective? If you want to remove a method that's in your scope from the list of delegates on the handler, you can just do..
object.Event -= new EventHandlerType(your_Method);
This will remove that method from the list of delegates, and you can reattach it later with
object.Event += new EventHandlerType(your_Method);
-
8I think you mean your_Method instead of your_Method(). As of C# 2.0, you also don't need the "new EventHandlerType" part - just object.Event += yourMethod; and object.Event -= yourMethod; – Jon Skeet Apr 13 '09 at 17:02
-
Yep, I meant for it to be sans parens ;). Was not aware of the implicit delegate construction, though; that's good to know. – Adam Robinson Apr 13 '09 at 17:07
-
This idea works for me(compared to accepted answer) in the scenario where I needed to disable the Handler temporarily for a combobox EditValueChanged. – PhoenixDev Sep 29 '15 at 7:25
I'm doing it in this way, using a control extension.
public static void SetDatasource(
this ComboBox cb,
object dataSource,
EventHandler _SelectedIndexChanged_eventHandler = null,
EventHandler _SelectedValueChanged_eventHandler = null)
{
//disable events
if (_SelectedIndexChanged_eventHandler != null) cb.SelectedIndexChanged -= _SelectedIndexChanged_eventHandler;
if (_SelectedValueChanged_eventHandler != null) cb.SelectedValueChanged -= _SelectedValueChanged_eventHandler;
if (cb.InvokeRequired)
{
cb.BeginInvoke(new _d_SetDatasource_ComboBox(SetDatasource), new object[] { cb, dataSource, _SelectedIndexChanged_eventHandler});
}
else
{
cb.DataSource = dataSource;
//events enabled again
if (_SelectedIndexChanged_eventHandler != null) cb.SelectedIndexChanged += _SelectedIndexChanged_eventHandler;
if (_SelectedValueChanged_eventHandler != null) cb.SelectedValueChanged += _SelectedValueChanged_eventHandler;
}
}
private delegate void _d_SetDatasource_ComboBox(ComboBox cb, object dataSource, EventHandler _SelectedIndexChanged_eventHandler = null, EventHandler _SelectedValueChanged_eventHandler = null);
Disabling the event for the component. Pseudocode:
YourComponent.YourComponentEvent -= ExistingMethodForTheEvent;
Enabling
YourComponent.YourComponentEvent += ExistingMethodForTheEvent;
Example for events like CellFormatting in DataGridView:
//enabling
DataGridView1.CellFormatting += DataGridView1_CellFormatting;
//disabling
DataGridView1.CellFormatting -= DataGridView1_CellFormatting;
private void DgvBillings_CellFormatting(...) {
...
}