logo
Учебник_ПОА

To create an event handler for a button

  1. Create a WPF application by using Visual C# Express Edition. For more information, see How to: Create a New WPF Application Project.

  2. Drag a Button from the Toolbox to the WPF window, and then select the button.

  3. Double-click the button.

The Click event handler is created and the cursor is positioned in the event handler in the Code Editor.

  1. Add the following code to the event handler:

    MessageBox.Show("Event handler was created by " + "double-clicking the button.");

  2. Drag a second Button control from the Toolbox to the WPF design surface, and then select the button.

  3. Add an attribute named Click to the Button element in the XAML editor, and set its value to ButtonOKClicked. This is the name that you will give the event handler in code. For example, the attribute can be written as follows: Click="ButtonOKClicked".

  4. Right-click the designer and then click View Code.

  5. Add the following event handler to the Window1 class. This code displays a message whenever you click the button.

    private void ButtonOKClicked(object sender, RoutedEventArgs e)

    {

    MessageBox.Show("Event handler was created manually.");

    }

  6. Press F5 to run the program.

  7. When the window appears, click a button.

  8. Verify that the correct text appears in a message box when you click each button, and then close the application.