|
The InitToolbar and UninitToolbar
procedures called from the
ThisApplication_Startup and
ThisApplication_Shutdown event handlers
are found in
ThisApplication.InitializeMenu.vb, a
class module that extends the
ThisApplication class by prefacing the
class declaration with the Partial
keyword for which VS 2005 adds support. This
class contains two private object variables
declared at the module level so that they do
not go out of scope and get released by
the .NET garbage collector:
CommandBarWithPositionInfo is the
separate class discussed
above
that facilitates the reading and saving of
toolbar position settings to the project
settings.
The InitToolbar procedure creates
a new toolbar and button in a manner that
should look familiar to VB6 developers --
check for the existence of the toolbar
first, create the toolbar if necessary
(setting its Temporary parameter to
True), then add the button.
The last argument in the statement to
create the toolbar sets the Temporary
parameter to True:
_showSettingsMessage = _toolbar.Controls.Add _
(Office.MsoControlType.msoControlButton, _
Type.Missing, Type.Missing, Type.Missing, True)
By recreating the toolbar anew for each
Outlook session, you handle the scenario
where the user uninstalls the add-in between
sessions. This, of course, is the reason for
providing a means to persist the toolbar
position settings between sessions: Since
the toolbar is temporary, its position is
not stored in the Outcmd.dat file with that
of other toolbars.
Because the code has to use the toolbar
name to determine if the toolbar already
exists, when we create the toolbar, we
protect it against the user changing the
name or adding/removing buttons with this
statement:
_toolbar.Protection = _
Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize
One thing that's different with this code
compared with the analogous technique in VB6
is how the event handler for the button's
Click event is set up. It is not
necessary to declare a CommandBarButton
object variable WithEvents. Instead,
this code statement assigns the Click
event for the _showSettingsMessage
button to an event handler procedure:
AddHandler _showSettingsMessage.Click, _
AddressOf _showSettingsMessage_Click
The name of the event handler is _showSettingsMessage_Click,
just as it would be for an event handler for
an object declared WithEvents, but it
is not necessary for the names to match. The
procedure could just as well have been named
ButtonClickHandlerDemo, as long as
the AddressOf operator points to a
procedure by that name. (TIP: This technique
can be extended to wire more than one event
to the same handler, for example, the Click
event for multiple option buttons on a
Windows form.)
Note that the button's object variable (_showSettingsMessage)
is declared at the module level, not at the
procedure level, to ensure that the variable
stays active for the life of the add-in. The _showSettingsMessage_Click
procedure shows a message box with
information about the current toolbar
position.
Also note how easy it is to use the
My.Settings.<property>
values to set the position and size
attributes for the toolbar, without any
explicit code to read from the user.config
file where they were stored by the
SaveSettings
procedure. |