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.
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.
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.
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
Now, perform the same steps 1 and 2 for the
MenuStrip1. Name its application setting
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 IfEnd 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 IfEnd Sub
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
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