1.5. Handle Exit and Close Events

Now it's time to write some code to link the Exit dialog with the events triggered by users when they attempt to exit or close the application.

  1. 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.

    Figure 1-21. Coding the File Exit menu item

    Figure 1-22. The code behind of Form1

    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.

  2. 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 If End 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.

  3. 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.

    Sideline Coloring

    The IDE in Visual Studio 2005 uses sideline coloring to highlight the lines that you have changed. The figure below shows the different colors that are displayed to the left of the code edit window after you enter the bold code in Example 1-2.

    The green color means that the lines have been modified and saved in the current edit session.

    The yellow color signifies that the lines have been changed but have not yet been saved. When you save the project or run the application, the yellow coding will turn green. When the project is closed and opened again in Visual Studio 2005, the green coding will disappear.

    Note that sideline coloring is activated only if the Track Changes option is set. By default, it is set on in the Visual Studio 2005/Team System Beta, but it is not set by default in VB Express.

    Figure 1-23. Generating the code stub for the FormClosing event

    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 If End Sub 

    Improved IntelliSense

    One of the most useful features of VB 2005 is the IntelliSense feature in Visual Studio. IntelliSense greatly reduces the amount of memorizing you need to do and automatically completes statements as you type.

    IntelliSense now performs smart filtering to list only the relevant methods, properties, and events of objects into two tabs Common and All so that you can quickly access the commonly used properties, methods, and events of an object without having to wade through the entire list (see figure).

    This improvement reduces the number of items showing up in an IntelliSense list, thereby making it easier for developers to search for what they want. If what you need cannot be found on the Common tab, you can always click on the All tab to see the complete list.

Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net