First, you'll add code to deal with the Exit menu item when a user selects it. To get started, expand the File menu in Form1 and doubleclick on the Exit menu item (see Figure 1-21) to open the code-behind page shown in Figure 1-22.
The code-behind page contains all of the code that you write for the application. So far in this chapter, all of the work has been done by Visual Studio, but the code it has generated to support your work with the designer and wizards is hidden and out of sight for now. You'll see how to view this code in Chapter 2.
When you double-click on the Exit item of the application menu on Form1, Visual Studio deduces that you wish to write code to specify how the exit event will be handled, just as it does in VB 6. Note that the code behind page in this example is named Form1.vb. As you can see in Figure 1-22, Visual Studio has already generated the code to create the event handler. All you need to do is specify what specific actions are to be taken when the event handler is called by placing your cursor on the line below the handler declaration and entering your own code.
Now, code the exit event of the
ToolStrip control by entering the code shown in bold in Example 1-2 on the code-behind page.
Example 1-2. Exit menu item event handler
Private Sub exitToolStripMenuItem_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles exitToolStripMenuItem.Click
Dim result As DialogResult = Dialog1.ShowDialog() If result = Windows.Forms.DialogResult.OK Then End End IfEnd Sub
When users select the Exit item on the File menu, an exit event fires and the code in Example 1-2 displays Dialog1, which forces users to confirm whether or not they really want to exit the application. If a user selects OK, then the application exits; otherwise, no action is taken and the dialog window closes.
Much of the code in Example 1-2 will seem familiar to VB 6 users.
Dim statements are still used to declare variables, the
If…Then statement is unchanged, and the
Sub…End Sub block is still used to define subroutines. What's new is the way in which the code makes use of classes, methods, and properties found in the .NET Framework, most visibly in the use of dot (.) syntax to reference them. You'll learn more about VB 2005 data types and language syntax in Chapter 2. Later in Chapter 3, you'll see how to use the new support for object-oriented programming and the .NET Framework in VB 2005 to greatly increase your productivity.
Now you need to add code to handle the case when the user closes the application window (Form1) by clicking on the Close button at top the right of the application windowt. This is handled by coding the
FormClosing event of Form1, as shown in Example 1-3. Again, when the user clicks the Close button, you'll display Dialog1. If the user selects the OK button, end the application. If the user selects the Cancel button, cancel the close operation by setting the
Cancel property of the
System.Windows.Forms.FormClosingEventArgs argument to
True. You can get Visual Studio 2005 to generate the code stub for the
FormClosing event by selecting the Form1 Events item in the left drop-down list (see Figure 1-23) and then selecting
FormClosing in the second drop-down list. Enter the code shown in bold in Example 1-3.
Example 1-3. FormClosing event handler
Private Sub Form1_FormClosing( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing
Dim result As DialogResult = Dialog1.ShowDialog() If result = Windows.Forms.DialogResult.OK Then End Else e.Cancel = True End IfEnd Sub