How to disable event in C#?

G

Gianco

Hello

I would know if is possible to obtain the event handler of an object,
store it in a variable, disable it and then, after some operations
enable it again

more or less something like this (the code is not C# but something
similar):

public void MakeSomeStuffsOnComboBox(ComboBox cmbID)
{
EventVariable EV;

EV = cmbID.SelectedIndexChanged;

cmbID.SelectedIndexChanged = null;

//
// some stuffs
//

cmbID.SelectedIndexChanged = EV;
}

I hope the example is enough clear!

Thanks in advance
Gianco
 
T

Tim Wilson

You could try something like this:

private void button1_Click(object sender, System.EventArgs e)
{
MakeSomeStuffsOnComboBox(this.comboBox1);
}

public void MakeSomeStuffsOnComboBox(ComboBox combo)
{
EventHandler handler = new EventHandler(comboBox1_SelectedIndexChanged);
combo.SelectedIndexChanged -= handler;
combo.SelectedIndex = 0;
combo.SelectedIndexChanged += handler;
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
MessageBox.Show("SelectedIndexChanged Called");
}
 
G

Gianco

Hello

Tim, thank you very much for your support

What I would to do is to write a generic routine that make some stuffs
on all the comboboxes in my project so in this routine I cannot
specify a particular handler but I must "extract" it from the combobox
properties. Is it possible?

Thank you
Gianco
 
J

Javier Campos

Gianco said:
Hello

Tim, thank you very much for your support

What I would to do is to write a generic routine that make some stuffs
on all the comboboxes in my project so in this routine I cannot
specify a particular handler but I must "extract" it from the combobox
properties. Is it possible?

Thank you
Gianco

The way I've always done it is having a global 'lockEvent' variable, or
either add it as a property on a custom control (which includes or inherits
from the ComboBox in your case), and a simple line as the first line in the
delegate:

if(this.lockEvent) return;

For your processing:

lockEvent = true;
foreach(Control cnt in myForm.Controls)
{
if(cnt is ComboBox)
{
// Do your stuff here
}
}
lockEvent = false;

This may sound obvious, but it's the way i've always done it, and works like
a charm :)

Hope this helps,

- Javier Campos
 
T

Tim Wilson

Ok, this might work a little better.

using System.Reflection;

private void button1_Click(object sender, System.EventArgs e)
{
PerformOperationsOnComboBox(this.comboBox1);
}

public void PerformOperationsOnComboBox(ComboBox ctrl)
{
FieldInfo info = ctrl.GetType().GetField("SelectedIndexChanged",
BindingFlags.Instance | BindingFlags.NonPublic);
if (info != null)
{
object obj = info.GetValue(ctrl);
if (obj is EventHandler)
{
EventHandler handler = (EventHandler)obj;
ctrl.SelectedIndexChanged -= handler;
// Perform operation here.
ctrl.SelectedIndexChanged += handler;
}
}
}
 
G

Gianco

Hello Javier

My dream is to avoid the use of global variables and compact
everything in specific functions but, however, this is a smart trick
to continue my work!

Thank you Javier
Gianco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top