Check if an event is already handled

M

mj2736

I created a windows form control that inherits from the standard .Net
DataGridView control, to which I've added custom functionality.
Instead of using the standard control in my applications, I use my
customized version. I have an event handler in the custom control for
the KeyDown event that simulates a double-click on the selected grid
row when the user presses the enter key. This is working just fine.

Now, some screens that are using this custom control handle the grid's
KeyDown event themselves, so in those cases I don't want the KeyDown
event handler in the custom control class to handle the event. Is
there some check I can perform in the custom control, without adding
anything to the screens that use it, to see if the event is already
being handled? Something like this:

private void MyDataGridView_KeyDown(object sender, KeyEventArgs e)
{
if ( /* check if the KeyDown event is already being handled
elsewhere */ )
{
return;
}
else
{
SimulateDoubleClick();
}
}

Apparently, GetInvocationList() is supposed to return all of the
handlers for an event, but "this.KeyDown.GetInvocationList()" does not
compile. Thanks!
 
P

pedrito

I created a windows form control that inherits from the standard .Net
DataGridView control, to which I've added custom functionality.
Instead of using the standard control in my applications, I use my
customized version. I have an event handler in the custom control for
the KeyDown event that simulates a double-click on the selected grid
row when the user presses the enter key. This is working just fine.

Now, some screens that are using this custom control handle the grid's
KeyDown event themselves, so in those cases I don't want the KeyDown
event handler in the custom control class to handle the event. Is
there some check I can perform in the custom control, without adding
anything to the screens that use it, to see if the event is already
being handled? Something like this:

private void MyDataGridView_KeyDown(object sender, KeyEventArgs e)
{
if ( /* check if the KeyDown event is already being handled
elsewhere */ )
{
return;
}
else
{
SimulateDoubleClick();
}
}

Apparently, GetInvocationList() is supposed to return all of the
handlers for an event, but "this.KeyDown.GetInvocationList()" does not
compile. Thanks!

My experience is with .NET 1.1 (no DataGridView), but since you're deriving
from it, I suspect you can override the OnKeyDown method instead of handling
the KeyDown event.

Then you could do something like this:

protected override void OnKeyDown(KeyEventArgs ke)
{
base.OnKeyDown(ke);
if (ke.Handled)
{
return;
}
... do your stuff here. ...
}
 
B

Ben Voigt [C++ MVP]

I created a windows form control that inherits from the standard .Net
DataGridView control, to which I've added custom functionality.
Instead of using the standard control in my applications, I use my
customized version. I have an event handler in the custom control for
the KeyDown event that simulates a double-click on the selected grid
row when the user presses the enter key. This is working just fine.

Now, some screens that are using this custom control handle the grid's
KeyDown event themselves, so in those cases I don't want the KeyDown
event handler in the custom control class to handle the event. Is
there some check I can perform in the custom control, without adding
anything to the screens that use it, to see if the event is already
being handled? Something like this:

private void MyDataGridView_KeyDown(object sender, KeyEventArgs e)
{
if ( /* check if the KeyDown event is already being handled
elsewhere */ )
{
return;
}
else
{
SimulateDoubleClick();
}
}

Apparently, GetInvocationList() is supposed to return all of the
handlers for an event, but "this.KeyDown.GetInvocationList()" does not
compile. Thanks!

No, that's for a delegate. A custom event cannot be treated as a delegate
field (although a delegate field can be used to create an event).
 
H

Herfried K. Wagner [MVP]

Now, some screens that are using this custom control handle the grid's
KeyDown event themselves, so in those cases I don't want the KeyDown
event handler in the custom control class to handle the event. Is
there some check I can perform in the custom control, without adding
anything to the screens that use it, to see if the event is already
being handled? Something like this:

private void MyDataGridView_KeyDown(object sender, KeyEventArgs e)
{
if ( /* check if the KeyDown event is already being handled
elsewhere */ )
{
return;
}
else
{
SimulateDoubleClick();
}
}

Apparently, GetInvocationList() is supposed to return all of the
handlers for an event, but "this.KeyDown.GetInvocationList()" does not
compile.

That's not possible for Windows Forms controls' events from outside the
class defining them because the delegate variable holding the handlers is
not exposed by the object model.
 
P

pedrito

Herfried K. Wagner said:
That's not possible for Windows Forms controls' events from outside the
class defining them because the delegate variable holding the handlers is
not exposed by the object model.
You can get to it through reflection and test the event handler for null. If
the event is handled, however, and the e.Handled is set to true, then he can
simply override the OnKeyDown handler and check for e.Handled.

I'm not saying reflection is always the best answer, but sometimes it does
come in handy.
 
M

mj2736

Thanks, I will research doing this through reflection.

I guess the KeyDown event is kind of a special case since there is the
Handled flag already in the event args, but really I'm looking for a
more general approach, since I would like to do the same type of thing
for other events that might not have such a flag in their event args.
 
P

pedrito

Thanks, I will research doing this through reflection.

I guess the KeyDown event is kind of a special case since there is the
Handled flag already in the event args, but really I'm looking for a
more general approach, since I would like to do the same type of thing
for other events that might not have such a flag in their event args.

Reflection isn't always a good "general approach", though in this case, it
may be the only choice.

It depends on who your users are and what their security settings are,
reflection is sometimes not allowed. Also, reflection can be slow, which is
another reason it's not always a good general approach.
 

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