| 1 | = Raise = |
| 2 | |
| 3 | Trigger an event to fire with the specified args. |
| 4 | |
| 5 | If the event is not null and any handlers are listening (registered with) [[BR]] |
| 6 | the event they will be invoked after the event has been raised. |
| 7 | |
| 8 | See also [wiki:Listen] and [wiki:Ignore]. |
| 9 | |
| 10 | == Grammar == |
| 11 | {{{ |
| 12 | raise <event> [ [,<source>] ,<event-args>] |
| 13 | }}} |
| 14 | |
| 15 | If the <source> is not specified but <event-args> is, then '''this''' |
| 16 | will be implicitly passed as the source. |
| 17 | |
| 18 | If an event has been declared with a no param signature |
| 19 | then neither <source> nor <event-args> can or need be given |
| 20 | as args to the raise call. |
| 21 | {{{ |
| 22 | sig NoArgEventHandler # no parameters |
| 23 | event notifyEvent as NoArgEventHandler # is public |
| 24 | ... |
| 25 | raise .notifyEvent |
| 26 | }}} |
| 27 | |
| 28 | == Examples == |
| 29 | {{{ |
| 30 | use System.Windows.Forms |
| 31 | |
| 32 | ... |
| 33 | button = Button(parent=p, autoSize=true, text='One', tag=1) |
| 34 | # connect event and handling code |
| 35 | listen button.click, ref .clickHandler |
| 36 | ... |
| 37 | # trigger the button programatically |
| 38 | raise button.click, EventArgs() |
| 39 | ... |
| 40 | # disconnect event and its handling code |
| 41 | ignore button.click, ref .clickHandler |
| 42 | ... |
| 43 | def clickHandler(source as Object, args as EventArgs) |
| 44 | pass |
| 45 | }}} |
| 46 | |
| 47 | |
| 48 | Full example with augmented !EventArg, signature and event declarations, |
| 49 | convenience method to raise the event, event handlers/listeners |
| 50 | {{{ |
| 51 | |
| 52 | # EventArgs subclass to pass additional info in args |
| 53 | class BEventArgs inherits EventArgs |
| 54 | var _value as int |
| 55 | |
| 56 | get value from var |
| 57 | |
| 58 | def init(val as int) |
| 59 | base.init |
| 60 | _value = val |
| 61 | |
| 62 | # delegate for ActionEvent <eventName>EventHandler |
| 63 | sig ActionEventHandler(sender as Object, args as EventArgs) |
| 64 | |
| 65 | class Generator |
| 66 | var _count = 0 |
| 67 | """local state""" |
| 68 | |
| 69 | # event dcl for event called Action |
| 70 | # event <eventName>Event as <sigName> (<eventName>EventHandler) |
| 71 | event actionEvent as ActionEventHandler |
| 72 | |
| 73 | # (protected) method to raise/fire the event for this class |
| 74 | # {on,fire}<eventName>Event |
| 75 | def _onActionEvent(args as EventArgs) # fireActionEvent |
| 76 | raise .actionEvent, args |
| 77 | |
| 78 | def bump |
| 79 | _count += 1 |
| 80 | if _count % 20 == 0 |
| 81 | ev = BEventArgs(_count) |
| 82 | _onActionEvent(ev) |
| 83 | |
| 84 | class Listener |
| 85 | # the event handler method do<eventName> |
| 86 | def doAction(source as Object, args as BEventArgs) |
| 87 | print 'Event from [source] - argCount is [args.count]' |
| 88 | |
| 89 | ... |
| 90 | class RaiseDriver |
| 91 | def main is shared |
| 92 | g = Generator() |
| 93 | l = Listener() |
| 94 | |
| 95 | #tie the generator event to the Listener method |
| 96 | listen g.actionEvent, ref l.doAction |
| 97 | |
| 98 | for i in 100 |
| 99 | g.bump |
| 100 | |
| 101 | #untie the event and listener |
| 102 | listen g.actionEvent, ref l.doAction |
| 103 | }}} |
| 104 | |
| 105 | No Arg event, No args passed to raise. |
| 106 | {{{ |
| 107 | # delegate with no args for notifyActionEvent <eventName>EventHandler |
| 108 | sig NoArgEventHandler |
| 109 | |
| 110 | class Generator |
| 111 | var _count = 0 |
| 112 | """local state""" |
| 113 | |
| 114 | # event dcl for event called Action |
| 115 | # event <eventName>Event as <sigName> (<eventName>EventHandler) |
| 116 | event notifyActionEvent as NoArgEventHandler |
| 117 | |
| 118 | # (protected) method to raise/fire the event for this class |
| 119 | # {on,fire}<eventName>Event |
| 120 | def _onNotifyActionEvent |
| 121 | raise .notifyActionEvent |
| 122 | |
| 123 | def bump |
| 124 | _count += 1 |
| 125 | if _count % 20 == 0, _onNotifyActionEvent |
| 126 | |
| 127 | class Listener |
| 128 | # the event handler method do<eventName> |
| 129 | def doNotifyAction |
| 130 | print 'NotifyAction Event received' |
| 131 | |
| 132 | ... |
| 133 | class RaiseDriver |
| 134 | def main is shared |
| 135 | g = Generator() |
| 136 | l = Listener() |
| 137 | |
| 138 | #tie the generator event to the Listener method |
| 139 | listen g.notifyActionEvent, ref l.doNotifyAction |
| 140 | |
| 141 | for i in 100 |
| 142 | g.bump |
| 143 | |
| 144 | #untie the event and listener |
| 145 | listen g.notifyActionEvent, ref l.doNotifyAction |
| 146 | }}} |
| 147 | |
| 148 | |
| 149 | == Notes == |