O'Reilly logo

Visual Basic 2005 Jumpstart by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Configure the Application

In the application that you have built, you can move and anchor the ToolStrip control as you wish while you are using the application. However, you may have noticed that its position is lost each time you exit the application. This is because you need to manually save its positions each time you exit the application, or else the information will be lost. Fortunately, this can be done easily with the new Application Settings feature in VB 2005. In this section, you'll see how.

Displaying the About box

Figure 1-34. Displaying the About box

  1. You'll begin by adding code to save the location of the ToolStrip control. Select the ToolStrip control and, in its Properties window, select the PropertyBinding property (under the "(ApplicationSettings)" property; see Figure 1-35). Click the "…" button.

    Binding application settings

    Figure 1-35. Binding application settings

  2. Locate the Location property (this property determines where the control should be placed) and click the drop-down listbox. Click on the New… link at the bottom and create a new application setting called ToolStripLocation (see Figure 1-36). Be sure to set the scope to User. Click OK. Be sure to set the Location property to the newly created application setting.

    Creating the new application setting

    Figure 1-36. Creating the new application setting

    You have now created an application setting that binds the position of the ToolStrip control to the ToolStripLocation application setting. When the form is loaded, the ToolStrip control will get its Location property from the application settings, which is saved in the app.config file.

    Tip

    The app.config file contains configuration settings pertaining to your project, such as database connection strings, application logfiles, etc. It is beyond the scope of this book to discuss the app.config file in detail.

  3. Now, perform the same steps 1 and 2 for the MenuStrip control, MenuStrip1. Name its application setting MenuLocation.

  4. You will want to save the current locations of all two controls whenever Form1 is closed, so add the bold code to the FormClosing event and Exit menu item event as shown in Example 1-5.

    Example 1-5. Saving control location data when a user exits or closes Form1

    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
    		 My.Settings.MenuLocation = MenuStrip1.Location
    		 My.Settings.ToolStripLocation = ToolStrip1.Location
    		 My.Settings.Save()
    	 	 End
    	 Else
    		 e.Cancel = True
    	 End If
    End Sub
    
    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
    		 My.Settings.MenuLocation = MenuStrip1.Location
    		 My.Settings.ToolStripLocation = ToolStrip1.Location
    		 My.Settings.Save()
    		 End
    	 End If
    End Sub

Tip

Note that in this example, only the ToolStrip control is moveable. You can make the MenuStrip control moveable by changing its GripStyle property from Hidden to Visible.

In Example 1-5, when a user closes the application either by closing the window or clicking on the Exit menu item, the application saves the position of the MenuStrip and ToolStrip controls using the application settings that you have created. You can access the application settings programmatically by using the My.Settings object (they automatically appear under the My.Settings object after you have created them). Once the locations of these two controls are assigned to the application settings, you use the Save method of the My.Setting object to persist this information to the app.config file.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required