Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Events are immutable. If you add or remove an event handler, a new event (or multicast delegate) is created. This also means, that you must be careful when adding events to a list. This will not work</p> <pre><code>public event EventHandler Event0; public event EventHandler Event1; public event EventHandler Event2; public List&lt;EventHandler&gt; Events; public void AddEvents( ) { Events = new List&lt;EventHandler&gt; { Event0, Event1, Event2 }; Event0 += new EventHandler(EventHandler0); Events[1] += new EventHandler(EventHandler1); } </code></pre> <p>If you add an event handler to <code>Event0</code> the corresponding list entry will still reference the old <code>Event0</code>! If you add an event handler to <code>Events[1]</code>, <code>Event1</code> will not be updated!</p> <hr> <p>I do not know if this is what you mean, however, if you have these two lists</p> <pre><code>public List&lt;EventHandler&gt; Events; public List&lt;Action&lt;object, EventArgs&gt;&gt; Actions; </code></pre> <p>You can add en event hander (an action) to an event like this:</p> <pre><code>Events[i] += new EventHandler(Actions[j]); </code></pre> <p>and raise an event like this</p> <pre><code>EventHandler eh = Events[i]; if (eh != null) { eh(this, EventArgs.Empty); } </code></pre> <hr> <p><strong>EDIT</strong> (in response to your comment):</p> <p>Here again, you must act on the original event list and not on an event passed as parameter, because of this immutable problem explained above. If necessary, you could also pass the list itself as an additional parameter.</p> <pre><code>public void AddActionToEvent(Action&lt;object, EventArgs&gt; action, int eventIndex) { Events[eventIndex] += new EventHandler(action); } </code></pre> <p>For your other problem of how to let the user select events and actions, I would suggest of either having four lists, where as the events and action lists would be paralleled with two corresponding string lists with the names of the events and actions. Alternatively, you could wrap the events and actions in a class containing them and their names</p> <pre><code>public class EventWrapper { public string Name { get; set; } public event EventHandler Event; public override string ToString() { return Name; } public void RaiseEvent(object sender) { EventHandler eh = Event; if (eh != null) { eh(sender, EventArgs.Empty); } } } public class ActionWrapper { public string Name { get; set; } public Action&lt;object, EventArgs&gt; Action { get; set; } public override string ToString() { return Name; } } </code></pre> <p>Then you would define the lists like this</p> <pre><code>public List&lt;EventWrapper&gt; EventsWithNames; public List&lt;ActionWrapper&gt; ActionsWithNames; </code></pre> <p>Now you can use these lists directly as data source in combo boxes or list boxes. The overridden <code>ToString</code> methods of the wrappers automatically provide a string to display.</p> <p>The add method can now be rewritten as</p> <pre><code>public void AddActionToEvent(ActionWrapper actionWrapper, EventWrapper eventWrapper) { eventWrapper.Event += new EventHandler(actionWrapper.Action); } </code></pre> <p>The wrappers also defuse the immutable situation, since we do not pass the event as parameter but its wrapper.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload