Step 3 of 4: Adding Code

In the previous step, you added some content to the control. In this step, you'll add some code to show a message when the switch is tapped, or turned on and off.

1.   In Design view, select the control and select View | Properties from the Visual Studio menu.

2.   In the Properties window, click the Events tab.

3.   Next to the Checked event, enter C1ToggleSwitch1_Checked. An event is created in the code.

4.   Enter the following code for the event. The code will show a message that the switch is off when tapped.

private void C1ToggleSwitch1_Checked(object sender, RoutedEventArgs e)

        {

            MessageBox.Show("Home Security System is now OFF.");

            C1ToggleSwitch1.Content = "Security System is OFF.";

        }

5.   Go back to the Visual Studio designer and locate the Unchecked event.

6.   Enter the following code for the event. The code will show a message that the switch is on when tapped.

private void C1ToggleSwitch1_Unchecked(object sender, RoutedEventArgs e)

        {

            MessageBox.Show("Home Security System is now ON.");

            C1ToggleSwitch1.Content = "Security System is ON.";

        }

Now run the application to see what happens when the switch is tapped.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.