If I have a combobox click event set in the designer.cs page and then at some point during the running of the program, based on some condition, I no longer want the combobox
Use the -= operator.
this.MyEvent -= MyEventHandler;
Your question indicates you don't have a good understanding of events in c# - I suggest looking deeper into it.
Assuming your handler is assigned like this:
this.comboBox1_Click += new System.EventHandler(this.comboBox1_Click);
disable it like this:
this.comboBox1.Click -= new System.EventHandler(this.comboBox1_Click);
The reason you cannot use
comboboxname.Click = null
or
comboboxname.Click += null
is that the event Click
actually contains a list of event handlers. There may be multiple subscribers to your event and to undo subscribing to an event you have to remove only your own event handler. As it has been pointed out here you use the -=
operator to do that.
//to subscribe
comboboxname.Click += ComboboxClickHandler;
//to conditionally unsubscribe
if( unsubscribeCondition)
{
comboboxname.Click -= ComboboxClickHandler;
}
Set:
comboBox.Click += EventHandler;
Unset:
comboBox.Click -= EventHandler;