Title | Save and restore toolbar configuration when a program starts and stops |
Description | This example shows how to save and restore toolbar configuration when a program starts and stops in Visual Basic 6. The program uses the Toolbar's SaveToolbar and RestoreToolbar methods. |
Keywords | toolbar, SaveToolbar, RestoreToolbar, configuration |
Categories | Controls, Software Engineering |
|
|
Note that WJK discovered a few bugs in the original version of this program:
I found three things wrong with the toolbar save restore process.
- The toolbar's savetoolbar method does not create the registry entry in proper location. MS 182943.
- The toolbar's restoretoolbar method needs a delay provided by a timer event. MS 191105.
- Microsoft's suggested 1 millisecond delay is not long enough for toolbars with a lot of buttons.
If you set a Toolbar control's AllowCustomize property to True, then the user can double-click on it to add, remove, and rearrange the Toolbar's buttons. This program saves and restores the configuration for particular users.
The form's Initialize event handler prompts for a user name and saves it in the m_UserName variable. The form's Load event handler enables the Timer1 timer control and sets its Interval property to 50 milliseconds.
|
|
Private m_UserName As String
Private Sub Form_Initialize()
' Get the user's name. In a more secure application,
' you would also get and verify a password.
m_UserName = InputBox("User Name", "User Name")
m_UserName = "Rod"
End Sub
Private Sub Form_Load()
'Interval=1 suggested by MicroSoft Workaround Article
' 191105
'I found that you need to Increase Interval When you
' have many buttons.
Timer1.Enabled = True
Timer1.Interval = 50
End Sub
|
|
The Timer1 control's Timer event handler calls the toolbar's RestoreToolbar method to restore the saved toolbar settings. It then disables the timer.
|
|
Private Sub Timer1_Timer()
' Revised Restore the Toolbar - MS Workaround Article
' 191105
' Notice how the two keys are separated During Restore.
tbrActions.RestoreToolbar _
"Software\howto_save_restore_toolbar", "Toolbar", _
m_UserName
Timer1.Enabled = False
End Sub
|
|
When the form unloads, its unload event handler calls the toolbar's SaveToolbar method to save the toolbar's settings.
|
|
Private Sub Form_Unload(Cancel As Integer)
' ' Original Save the Toolbar state - per VB
' documentation
' tbrActions.SaveToolbar _
' "Software\howto_save_restore_toolbar", _
' "Toolbar", m_UserName
' MS Article 182943
' The key argument of the SaveToolbar method is ignored.
' The following example will actually store the value
' "Toolbar1" at the registry key HKEY_CURRENT_USER\User1
' rather than HKEY_CURRENT_USER\AppName\User1 as you might
' expect:
' Toolbar1.SaveToolbar "AppName", "User1", "Toolbar1"
' Revised Save the Toolbar state - MS Workaround
' Notice how two keys were combined for save.
tbrActions.SaveToolbar _
"", "Software\howto_save_restore_toolbar\Toolbar", _
m_UserName
End Sub
|
|
When the user double-clicks the Toolbar, the Toolbar Configuration dialog displays a list of buttons that can be added and removed from the Toolbar. If the user has removed some of the buttons and then the program called SaveToolbar, those buttons do not appear in the list of buttons that can be added when the program restarts. (At least on my system. Microsoft's documentation seems to imply that they should be there.)
The user can restore all of the buttons by clicking the dialog's Reset button. The user must then remove any undesired buttons and rearrange the remaining buttons. Cumbersome but better than nothing.
|
|
|
|