Adding Event Handlers

A menu item is only useful if it does something when selected. A menu item is just like other OpenLaszlo objects. All you need to do to make it react to an event is add an event handler to its selected event.

The code in Listing 9.4 shows an LZX application with menu items that resize a window when selected.

Listing 9.4. Responding to selection
<canvas>
    <window id="win" title="Demo" width="150" height="200"/>
    <menubar>
        <menu text="Action" width="100">
            <menuitem text="Minimize">
                <method event="onselect">
                    win.setWidth(50);
                    win.setHeight(50);
                </method>
            </menuitem>
            <menuitem text="Maximize">
                <method event="onselect">
                    win.setWidth(150);
                    win.setHeight(200);
                </method>
            </menuitem>
        </menu>
    </menubar>
</canvas>

Use this URL to compile the program in Listing 9.4.

http://localhost:8080/lps-4.0.x/app09/menuTest4.lzx

Figure 9.5 shows the result.

Figure 9.5. Menu items with event handlers


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset