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

Figure 1-34. Displaying the About box


Application Settings

In VB 2005, you can now save the state of your application. For example, if a user resizes your application window, then your application can "remember" the last displayed size, if you take the appropriate steps. Using the new Application Settings feature in Windows Forms 2.0, you can create application settings for your application and then bind your controls (including Windows Forms) to these application settings via the Properties window.


  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.

    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.

    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.

    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 

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.

The My Namespace

The use of My.Settings in Example 1-5 demonstrates one of the most useful and unique additions to VB 2005, the new My namespace, which encapsulates some of the most common functionalities that developers need for their daily work. The My namespace exposes several different objects, which you can observe by going to the code-behind page (Form1.vb) and typing My.. IntelliSense shows the objects of the My namespace as shown in the figure.

The aim of the My namespace is to provide direct access to commonly used libraries in the .NET Framework that were previously difficult to access. The intuitive hierarchy of the My namespace provides a mechanism that VB 2005 developers can use to easily navigate the .NET Framework class libraries and locate the classes required to perform a particular task. For example, suppose you want to play an audio file in your application. But which class should you use? Using the My namespace, it is easy to locate the right class to use. As it turns out, the class to use can be found in My.Computer.Audio.Play!

You will learn more about the My namespace in Chapter 2.




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