ADDING CODE TO CONTROLS

After you have added the appropriate controls to a form and set their properties, the next step is to add code to the form that responds to control events and manipulates the controls.

You can add some kinds of code declaratively in the XAML editor. For example, you can make a trigger respond to a change in a control’s property or to a control’s event.

You can also write Visual Basic source code to respond to control events just as you would in a Windows Forms application. If you double-click a control on the WPF Designer, Visual Studio creates an empty event handler to catch the control’s default event, and it opens the event handler in the code editor.

For example, the following code shows the event handler the IDE built for a Button control. The default event for a Button is Click, so this code is a Click event handler.

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
 
End Sub

SIT BACK AND RELAX
As is the case with Windows Forms, you can use relaxed delegates to remove unneeded parameters from event handlers. For example, the following code shows the previous event handler with the unnecessary parameters removed:
Private Sub Button_Click()
 
End Sub

Another way to build an event handler and open the code editor is to select the control on the WPF Designer. Then click the Events icon (the lightning bolt) near the top of the Properties window to make the window show a list of events for the control. Double-click an event in the window to open a new event handler for it in the code editor.


ATTACHMENT VARIATIONS
How the event is attached to the control depends on whether the control has a name. If the control’s Name property is set to some value, Visual Studio uses a Handles clause in the Visual Basic code to indicate the control that uses the event. If the control does not have a name, Visual Studio defines the connection between the control and the event handler in the XAML code.

You can also create a new event handler for named controls within the code editor. The upper-left part of the code editor displays a drop-down listing the window’s controls. If you select a control from the list, you can then pick an event for that control from a second drop-down in the code editor’s upper right. If you select an event, the code editor makes a corresponding empty event handler.

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

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