Edit

Share via


Add an event handler

From the resource editor, you can add a new event handler, or edit an existing event handler, for a dialog box control using the event handler wizard.

You can add an event to the class implementing the dialog box using the Properties window. To add the event to a class other than the dialog box class, use the event handler wizard.

To add an event handler to a dialog box control:

  1. Double-click the dialog box resource in Resource View to open the dialog box resource that contains the control in the dialog editor.

  2. Right-click the control for which you want to handle the notification event.

  3. On the shortcut menu, choose Add Event Handler to display the Event Handler Wizard.

  4. Select the event in the Message type box to add to the class selected in the Class list box.

  5. Accept the default name in the Function handler name box, or provide the name of your choice.

  6. Select Add and edit to add the event handler to the project and open the text editor at the new function to add the appropriate event handler code.

    If the selected message type already has an event handler for the selected class, Add and edit is unavailable, and Edit code is available. Select Edit code to open the text editor at the existing function.

Alternately, you can add event handlers from the Properties window. For more information, see Adding event handlers for dialog box controls.

In this section

Event handler wizard

This wizard adds an event handler for a dialog box control to the class of your choice. If you add an event handler from the Properties window, you can add it only to the class that implements the dialog box. For more information, see Adding event handlers for dialog box controls.

  • Command name

    Identifies the selected control, for which the event handler is added. This box is unavailable.

  • Message type

    Displays the list of current possible message handlers for the selected control.

  • Function handler name

    Displays the name of the function that's added to handle the event. The name by default is based on the message type and the command, prepended by On. For example, for the button called IDC_BUTTON1, the message type BN_CLICKED displays the function handler name OnBnClickedButton1.

  • Class list

    Displays available classes to which you can add an event handler. The class for the selected dialog box is displayed in red.

  • Handler description

    Provides a description for the item selected in the Message type box. This box is unavailable.

  • Add and edit

    Adds the message handler to the selected class or object. It also opens the text editor to the new function so you can add the handler code for the control notification.

  • Edit code

    Opens the text editor to the selected existing function so you can add or edit the control notification handler code.