WINDOW APPLICATIONS

A typical desktop WPF application displays its controls in Window objects. To create this type of application, select the File menu’s New Project command to display the New Project dialog box. On the Visual Basic ⇒ Windows tab, select WPF Application, enter a project name, and click OK.

The new application begins with a single Window class named Window1. Open the Solution Explorer and double-click the Window1.xaml entry to edit the Window’s controls. Double-click the Window1.xaml.vb entry to edit the Visual Basic code behind the Window.


CODE-BEHIND
The code behind a Window is called its code-behind. It’s not a very imaginative term, but it’s easy to remember.

To add other Window classes, open the Project menu and select Add Window. Enter a name for the class and click OK.

To display a window in code, create a variable that refers to a new instance of the window. Call its Show method to display the window non-modally, or call its ShowDialog method to display the window modally. The following code creates a new window of type Window2 and displays it modally:

Dim win2 As New Window2()
win2.ShowDialog()

Although several similarities exist between the way a program uses a Window and the way it uses a Form, there are many significant differences.

For example, both classes have a DialogResult property that indicates how the user closed the form. Both classes’ ShowDialog methods return this result, so the code can easily determine the form’s DialogResult value. In a Form, the DialogResult property is a value of type DialogResult, an enumerated type that provides values such as OK, Cancel, Yes, and No to indicate which button the user clicked to close the form. If the code sets this value, the form automatically hides, so the calling ShowDialog method returns.

In contrast, a WPF Window’s DialogResult value is a Boolean intended to indicate whether the user accepted or canceled the dialog box. If you need more detail (did the user click Yes, No, or Cancel?), you’ll need to provide code in the dialog box to remember which button the user clicked. If the code sets DialogResult, the window automatically closes so the calling ShowDialog method returns. Unfortunately, the window closes rather than hides so you cannot display the dialog box again. (You cannot display a window after it has closed.) If you want to remember which button the user clicked and then hide the window without closing it, you’ll need to implement your own property rather than DialogResult, and you’ll need to hide the window explicitly.

The Windows Forms and WPF Button classes also both have properties that you can use to define a dialog box’s default and cancel buttons, but they work in different ways.

You can set a Windows Forms Button object’s DialogResult property to the value you want the button to give to the form’s DialogResult property. If the user clicks the button, it assigns the form’s DialogResult value and hides the form so the calling ShowDialog method returns that value.

In a WPF application, you can set a button’s IsCancel property to True to indicate that the button is the form’s cancel button. If the user presses the Escape key or clicks the button, the button sets the form’s DialogResult property and closes the form so the calling ShowDialog method returns. Unfortunately, the button closes the form rather than merely hiding it so, as before, you cannot display the dialog box again.

You can also set a WPF button’s IsDefault property to indicate that it should fire if the user presses the Enter key. Unfortunately, this does not automatically set the form’s DialogResult property and does not close the dialog box.

Example program UseDialog, which is available for download on the book’s website, shows one approach to solving this problem. The dialog class Window2 contains three buttons labeled Yes, No, and Cancel.

The following code shows how the dialog box handles button clicks. The single btn_Click event handler fires for all three of the buttons. It saves the button’s text in the public variable UserClicked and then closes the form.

Partial Public Class Window2
    Public UserClicked As String = "Cancel"
 
    Private Sub btn_Click(btn As Button, e As RoutedEventArgs) _
     Handles btnYes.Click, btnNo.Click, btnCancel.Click
       UserClicked = btn.Content
       Me.Close()
    End Sub
End Class

The following code shows how the program’s main window displays the dialog box and checks the result. When you click the Show Dialog button, the program creates a new dialog window and displays it modally. It then checks the dialog box’s UserClicked property to see which button the user clicked.

Private Sub btnShowDialog_Click() Handles btnShowDialog.Click
    Dim win2 As New Window2()
    win2.ShowDialog()
    Select Case win2.UserClicked
        Case "Yes"
            MessageBox.Show("You clicked Yes", "Yes", MessageBoxButton.OK)
        Case "No"
            MessageBox.Show("You clicked No", "No", MessageBoxButton.OK)
        Case "Cancel"
            MessageBox.Show("You clicked Cancel", "Cancel", _
                MessageBoxButton.OK)
    End Select
End Sub

Most of the things that you can do with a Form you can do with a Window. For example, you can:

  • Create new instances of Window classes.
  • Display Windows modally or non-modally.
  • Close or hide Windows.
  • View and manipulate the properties of one Window from within the code of another Window.

Nevertheless, the details of Form and Window operations may be different. You may need to use slightly different properties, and you may need to take a slightly different approach, but Window is a fairly powerful class and with some perseverance you should be able to build usable interfaces with it.

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

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