CommandBars Articles and Tutorials

Using Commandbar Actions

Author: Mike Palmatier
Platform: Visual Basic 6.0

CommandBar Actions eliminate the need to use the Update event to update\change the state and properties of an item that appears multiple places in your menus and toolbars. For example, you might have a menu item that also appears in a toolbar, then a user might have created the same button and placed it in a user created toolbar. With Actions you can simply modify the action for the item and it will update all occurrences of the item, no matter how many a user might have added.

Actions also work good for localization as you can have a set of actions for each local that can easily be used to update all occurrences of items ensuring that all items get updated.

How it works:
Each CommandBar item has an Id. Items with the same Id can then share a common set of properties. A CommandBars Action object, represented as "Item Actions" in the diagram below, is a set of properties that all items of a specified Id can share. When you add an Action you will specify the Id, Caption, TooltipText, DescriptionText, and Category of the action. The Id is the Id you will assign to CommandBar items that you cant to use the properties of the action. Now any CommandBar item with the same Id as an Action will use the properties of the action. When updating any property of the action it will automatically update all occurrences of the item, no matter how many a user might have added, if it is in a popup menu, or in a toolbar. You will no longer have to use the Update event to update\change the state and properties of an item that appears multiple places in your menus and toolbars.

The EnableActions method must be called to enable actions to be used. Each CommandBar Item has an Action property which is just a reference to the CommandBars Action item that is associated with it. If no action is added for an item or actions have not been enable the default properties of the item will be used.

In the diagram below, the "Menu Item," "Toolbar Item" and "User Customized Item (user created toolbar)" all share the same "Item Action." When you modify the properties of the action it will update all occurrences of the item.

Share same icon

'Constants used to identify controls
Const ID_FILE_NEW         = 100
Const ID_FILE_OPEN        = 101
Const ID_FILE_CLOSE       = 102
Const ID_FILE_SAVE        = 103
Const ID_FILE_EXIT        = 104
Const ID_FILE_PRINT       = 113
Const ID_FILE_PRINT_SETUP = 114

Private Sub Form_Load()

'Enable the use of actions
CommandBars.EnableActions

'Create Actions to be used by CommandBar items. You can create
'the action before or after the CommandBar Items are added, but
'if you create them before like this sample be sure not to 
'overwrite any of the 5 properties of the actions when the 
'CommandBar item is added.
CommandBars.Actions.Add ID_FILE_NEW, "&New", "New", _
    "Create a new document", "File"
CommandBars.Actions.Add ID_FILE_OPEN, "&Open", "Open", _
    "Open an existing document", "File"
CommandBars.Actions.Add ID_FILE_SAVE, "&Save", "Save", _
    "Save the active document", "File"
CommandBars.Actions.Add ID_FILE_PRINT, "&Print", "Print", _
     "Print the active document", "File"
CommandBars.Actions.Add ID_FILE_PRINT_SETUP, "&Print Setup", _
    "Print Setup", "Change the printing options", "File"
CommandBars.Actions.Add ID_FILE_CLOSE, "&Close", "Close", _
    "Close the active document", "File"
CommandBars.Actions.Add ID_FILE_EXIT, "&Exit", "Exit", _
    "Quit the application; prompts to save documents", "File"

Dim Control As CommandBarControl
Dim ControlFile As CommandBarPopup

'Add some CommandBar items.
Set ControlFile = CommandBars.ActiveMenuBar.Controls.Add( _
                                xtpControlPopup, 0, "&File")
With ControlFile.CommandBar.Controls
    .Add xtpControlButton, ID_FILE_NEW, ""
    .Add xtpControlButton, ID_FILE_OPEN, ""
    .Add xtpControlButton, ID_FILE_SAVE, ""
    Set Control = .Add(xtpControlButton, ID_FILE_PRINT, "")
    Control.BeginGroup = True
    .Add xtpControlButton, ID_FILE_PRINT_SETUP, ""
    Set Control = .Add(xtpControlButton, ID_FILE_EXIT, "")
    Control.BeginGroup = True
End With

Dim ToolBar As CommandBar
Set ToolBar = CommandBars.Add("Standard", xtpBarTop)
With ToolBar.Controls
    .Add xtpControlButton, ID_FILE_NEW, ""
    .Add xtpControlButton, ID_FILE_OPEN, ""
    .Add xtpControlButton, ID_FILE_SAVE, ""
    Set Control = .Add(xtpControlButton, ID_FILE_PRINT, "")
    Control.BeginGroup = True
End With

'Disable ALL items with Id ID_FILE_SAVE
CommandBars.Actions(ID_FILE_SAVE).Enabled = False

'Change the Caption of ALL items with Id ID_FILE_PRINT
CommandBars.Actions(ID_FILE_PRINT).Caption = _
    "Print Document"

'Change the ToolTipText of ALL items with Id ID_FILE_OPEN
CommandBars.Actions(ID_FILE_OPEN).ToolTipText = _
    "Open an existing document"
End Sub