Best practices

  • Thread starter Thread starter Ian Williamson
  • Start date Start date
I

Ian Williamson

Greetings,

I have a custom control which I am adding to a form. It
processes Key events, but if the event is not handled at
this control's level, then it needs to pass the event up
to the containing form.

Currently I am making a call similar to:

((MyForm)((Control)sender).TopLevelControl).MyForm_KeyUp
(sender, e);


I find this a rather nasty way to approach this as it
ruins encapsulation for my control since it needs
explicit knowledge of the form that it will be contained
in.

Is there is a better way to throw this event to the
parent form?

Cheers, Ian Williamson
 
In my custom controls, I simply call the base handler using the base
keyword. Like this:

protected override void onKeyUp(KeyEventArgs e)
{
// do some stuf

if (!handledByMe)
base.OnKeyUp (e);
}
 
But I am not looking for the base control to handle the
event. I want to pass the event up to the form level
event handlers.

Dispatching events to other controls the way it appears
to be setup in .NET requires explicit knowledge of both
the calling control class name and method that it exposes
to handle the event.

Is there some other more elegant way of doing this?
 
Back
Top