I have a form with controls, and this form should raise own events
different from events of the controls. But I have difficulties to
connect the control events and the Form events in a flexible way (the
problem is not specific to Windows.Forms but the handling of events in
.NET generally).

Long story:

In my form I want to transform the "technical" events of the controls
("button clicked", "mouse moved" etc.) in more "logical" events ("user
wants this", "user wants that"). If the controls on my form raise
events they should be handled in the form. The form can do some work
(e.g. data moving) and can raise own events thereafter.

To be flexible I want to connect the technical and logical events
in a small class "EventConnector".

Here's an excerpt of the source code (I haven't run this example so
there might be syntax errors, but the more elaborated version works!):

,----
| using System.Windows.Forms
|
| class MyForm: Form
| {
|    // Delegate and Event for the logical event
|    public delegate void MyFormDelegate( MyForm sender, EventArgs args );
|    public event MyFormDelegate MyEvent;
|
|    // Inner Class
|    private class EventConnector
|    {
|       private readonly MyFormDelegate myFormEvent; // logical event
|       private readonly MyForm myForm;
|
|       public EventConnector( MyForm form, MyFormDelegate formEvent )
|       {
|          myForm = form;
|          myFormEvent = formEvent;
|       }
|
|       // This method handles the events of the controls
|       public void Handle( object sender, EventArgs e )
|       {
|           myForm.DoSomeWork();
|           myFormEvent( myForm, e );
|       }
|    }
|
|    private Button button;
|
|    private void InitializeComponent()
|    {
|        button = new Button();
|        ... // set location etc.
|        EventConnector connector = new EventConnector( this, MyEvent );
|        button.Click += new EventHandler( connector.Handle );
|    }
|
|    private void DoSomeWork()
|    {
|       ...
|    }
| }
`----

This works - partly :-/ . It works *only* if a listener is subscribed
to MyForm.MyEvent before creating the EventConnector. If nobody is
subscribed to MyForm.MyEvent it has the value null. If you first
create the EventConnector and then someone subscribes to
MyForm.MyEvent, the EventConnector got and still has a null for
myFormEvent.

The event "springs into existence" only if someone subscribes to it so
events don't behave like normal objects!?

Because I have a lot of forms I need a flexible and central way to
handle the mapping from technical to logical events. If I want to do
it centrally (e.g. in a base class) I have to pass the logical event
as parameter to a method or constructor. But it doesn't work if the
subscribing to the logical event happens after passing the event.

How can I solve my problem? Or is there generally a better
aproach/design to do the mapping from technical to logical event?

Thanks,

        Burkhard

--
Burkhard Perkens-Golomb                 mailto: [EMAIL PROTECTED]
sd&m AG                                 http://www.sdm.de
software design & management AG
Carl-Wery-Str. 42, 81739 Muenchen, Germany
Tel +49 89 63812-398, Fax -410
Tel Home +49 89 66 00 09 61

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

Guerrilla ASP.NET, 10 Nov 2003 in London and 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnet

Guerrilla .NET, 8 Dec 2003, in Los Angeles
http://www.develop.com/courses/gdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to