registerevent - Register event handler for COM object event at run-time

Syntax

h.registerevent(event_handler)
registerevent(h, event_handler)

Description

h.registerevent(event_handler) registers certain event handler routines with their corresponding events. Once an event is registered, the object responds to the occurrence of that event by invoking its event handler routine. The event_handler argument can be either a string that specifies the name of the event handler function, or a function handle that maps to that function. Strings used in the event_handler argument are not case sensitive.

registerevent(h, event_handler) is an alternate syntax for the same operation.

You can either register events at the time you create the control (using actxcontrol), or register them dynamically at any time after the control has been created (using registerevent). The event_handler argument specifies both events and event handlers (see Writing Event Handlers in the External Interfaces documentation).

Examples

Register Events Using Function Name Example

Create an mwsamp control and list all events associated with the control:

f = figure ('position', [100 200 200 200]);
h = actxcontrol ('mwsamp.mwsampctrl.2', [0 0 200 200], f);

h.events
ans = 
   Click = void Click()
   DblClick = void DblClick()
   MouseDown = void MouseDown(int16 Button, int16 Shift,  
      Variant x, Variant y)

Register all events with the same event handler routine, sampev. Use eventlisteners to see the event handler used by each event:

h.registerevent('sampev');
h.eventlisteners
ans = 
    'click'        'sampev'
    'dblclick'     'sampev'
    'mousedown'    'sampev'

h.unregisterallevents;

Register the Click and DblClick events with the event handlers myclick and my2click, respectively. Note that the strings in the argument list are not case sensitive.

h.registerevent({'click' 'myclick'; ...
                 'dblclick' 'my2click'});
h.eventlisteners
ans = 
    'click'       'myclick' 
    'dblclick'    'my2click'

Register Events Using Function Handle Example

Register all events with the same event handler routine, sampev, but use a function handle (@sampev) instead of the function name:

h = actxcontrol('mwsamp.mwsampctrl.2', [0 0 200 200]); 
registerevent(h, @sampev);

Register Workbook Events Example

Create a Microsoft® Excel® Workbook object.

myApp = actxserver('Excel.Application');
wbs = myApp.Workbooks;
wb = wbs.Add;

Register all events with the same event handler routine, AllEventHandler.

wb.registerevent('AllEventHandler') 
wb.eventlisteners

MATLAB® displays the list of all Workbook events, registered with AllEventHandler.

ans = 

    'Open'                'AllEventHandler'
    'Activate'            'AllEventHandler'
    'Deactivate'          'AllEventHandler'
    'BeforeClose'         'AllEventHandler'
                 .
                 .

See Also

events (COM), eventlisteners, unregisterevent, unregisterallevents, isevent

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS