Chapter 8: Building Desktop Applications with WPF 233

The previous code shows both the old button1_Click (Button1_Click in VB) event
handler and the new NewOrderButton_Click event handler. You might wonder why the
button1_Click event handler wasn’t deleted when you deleted it from the Click event in
the Properties window, but there’s a good reason for this. What if you had already written
code in the event handler? VS leans toward the safe side and does not delete your code.
Using the previous steps, you have both event handlers sitting side-by-side, which means
that you can easily copy your code from button1_Click into NewOrderButton_Click and
then delete the button1_Click event handler. So far, we haven’t written any code for the
event handler, which you’ll learn about in the next section.
Coding Event Handlers
One of the tasks you might want to do when a user clicks a button is to open a new
window. The first thing you’ll need to do is add a new window. To make this work, you
would open Solution Explorer, right-click the project you’re working with, select Add |
New Item, choose Window (WPF), name the window NewOrder.xaml, and click Add.
This will create a new window open in the Designer.
TIP
The project’s Add | New Item context menu includes a Window entry, which can save a
couple of clicks when creating a new window.
After the Designer loads, you can quickly open the code-behind by pressing F7. In the
code-behind, you’ll see the following code:
C#:
public partial class NewOrder : Window
{
public NewOrder()
{
InitializeComponent();
}
}
VB:
Public Class NewOrder
End Class