assign an event to a button with handling function as a parameter

G

Guest

Hi,

I would like to do have the following method:
AssignMethodToButtonClick(myButton_Click, myButton);

This would be equivalent do:
myButton.Click+=new EventHandler(myButton_Click);

Why... To add keyboard support to my application I want to make a class that
will assign events to controls in a choosen form. Now depending on which form
the user is, I handle the KeyDown events differently (switch... case...).

Currently I have to copy the whole thing into each single form, but I would
like to make a clean separate class and be able to change the handling
function at runtime.

How could I do this?

Thanks,
Sitar.
 
D

dwhittenburg

I'm not sure if this helps, but can the Observer pattern be used?

Can you have a class1 that delegates (there is a reserved word 'delegate'
and 'event' that can be used) the eventhandler off to another class2 and
then the other class2 registers with the event handler of class1? Thus when
the event fires for class1 it will call the function class 2 which has been
registered...

I have not tried this in the CF, but I know it works well in C#...
Hope this is what you were asking...
 

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