Handling Events

Most of the code in the MyNotepad application occurs in the event handler methods that are called when menu items are clicked. The Forms Designer makes it easy to create the skeleton event handlers for events generated by controls on the form.  For example, to add a handler method for menu click events, you just double-click the menu item on the form. (While double-clicking directly on a button or a menu item produces the click event handlers, you can just as easily create hander skeleton code for other events by using the Events tab in the Properties window).

The Forms Designer then adds a skeleton event handler method, in which you can add code, to your form’s class, and inserts a MenuItem.addOnClick call for the appropriate MenuItem class in initForm.

For example, when you click on a menu item named FileMenuNew, the following line is added to initForm and a method called FileMenuNew_click is added to your class:

      FileMenuNew.addOnClick(new EventHandler(this.FileMenuNew_click));

The MenuItem.addOnClick method takes an EventHandler object. The EventHandler object is created with a reference to the method to call when that menu item is clicked. Essentially, the MenuItem object monitors mouse clicks and calls each event handler added to it, using the delegate, when the event occurs. 

All event handler objects are delegates; the difference between them is the event object they pass to the handler. An EventHandler delegate passes an Event object, which contains information about the event. But a KeyEventHandler, for example, passes a KeyEvent object that extends Event for keydown and keyup events. (KeyEvent contains an extra field specifying the UNICODE character and whether it was combined with a CTRL, SHIFT, or ALT key.)

Most WFC programmers need to know little about delegates because event handlers already exist in the WFC packages for the events they care about and the code to add them is generated by the Forms Designer for the most part.

The following is the event handler in the MyNotepad application that handles the selection of this particular menu item:

    private void FileMenuNew_click(Object sender, Event e)
    {
        // If edit control contains text, check if it should be saved
        if (editbox.getText().length() != 0) {       
            // Open NewDialog class as a modal dialog
            int result = new NewDialog().showDialog(this);    
            // Retrieve result 
            // If Yes button was clicked open Save As dialog box
            if (result == DialogResult.YES)
                this.FileMenuSaveAs_click(sender, e);
            // If No button was clicked clear edit control and set title
            else if (result == DialogResult.NO) {
                editbox.setText("");
                this.setText("Untitled - MyNotepad");
            }
        }   
    }

Of course, the Forms Designer creates just the skeleton event handler. The code that opens the custom modal dialog box was added manually.