English | Русский | UserForms-Class-ALL
clsContextMenu is a VBA class that implements a customizable context menu for use in Excel VBA UserForms. The class manages CommandBar controls, creating a flexible menu system that can be bound to various UI elements such as ListBox, TextBox, and UserForm controls with the ability to dynamically add, remove, and modify menu items.
- Multiple control types: Support for buttons, edit fields, dropdowns, comboboxes, and popup menus
- Dynamic management: Ability to add, remove, and modify menu items at runtime
- Flexible binding: Can be attached to different types of controls (ListBox, TextBox, UserForm)
- Tag-based identification: Items can be identified and managed using tags
- Event handling: Automatic handling of right-click events to show the context menu
- Microsoft Excel with VBA support
- Windows operating system for CommandBar functionality
- Copy the
clsContextMenu.clsfile to your VBA project - Import the class in the VBA editor
Dim myContextMenu As New clsContextMenu
' Initialize context menu
Call myContextMenu.Initialize("MyCustomMenu")
' Bind to a control (ListBox, TextBox, or UserForm)
Set myContextMenu.prBoundControl = ListBox1
' Add controls to the context menu
Call myContextMenu.addControl(enumTypeControl.enControlButton, "Option 1", "macroName", 45)
Call myContextMenu.addControl(enumTypeControl.enControlButton, "Option 2", "anotherMacro", 46, "tag1")' Show the context menu programmatically
myContextMenu.showMenu()
' Add different types of controls
Call myContextMenu.addControl(enumTypeControl.enControlEdit, "Edit Field", "editMacro", 130)
Call myContextMenu.addControl(enumTypeControl.enControlComboBox, "Combo Box", "comboMacro", 589)
Call myContextMenu.addControl(enumTypeControl.enControlDropdown, "Dropdown", "dropMacro", 589)
' Manage controls by tag
Dim control As CommandBarControl
Set control = myContextMenu.getControlByTag("tag1")
If Not control Is Nothing Then
control.Caption = "Updated Option"
End If
' Delete a control by tag
myContextMenu.deleteControlByIdTag("tag1")' Initialize context menu
Call myContextMenu.Initialize("SharedMenu")
' Bind to multiple controls
Set myContextMenu.prBoundControl = ListBox1
Set myContextMenu.prBoundControl = TextBox1
Set myContextMenu.prBoundControl = Me ' The UserForm itself| Property | Type | Description |
|---|---|---|
prContextMenuName |
String | Name of the context menu |
prCountControls |
Integer | Number of controls in the context menu |
prBoundControl |
Object | Sets the control to which the context menu is bound |
prContextMenu |
CommandBar | Gets the underlying CommandBar object |
| Method | Description |
|---|---|
Initialize |
Initializes the context menu with a specified name |
addControl |
Adds a control to the context menu |
getControlByTag |
Retrieves a control by its tag |
getControlByIndex |
Retrieves a control by its index |
getControlByIdTag |
Alternative method to retrieve a control by its tag |
deleteControlByIdTag |
Deletes a control by its tag |
deleteControlByIndex |
Deletes a control by its index |
showMenu |
Programmatically displays the context menu |
Version |
Returns class version information |
| Enum | Values | Description |
|---|---|---|
enumParametrVersion |
enName, enAuthor, enVersion, enLicense, enDateOfCreation, enDateOfUpdate, enDescription, enAll | Used to retrieve version information |
enumTypeControl |
enControlButton, enControlEdit, enControlDropdown, enControlComboBox, enControlPopup | Specifies the type of control to add to the context menu |
Usage example is presented in the frmTestClass.frm form, which demonstrates all class capabilities.
To run the example:
- Open the
context_menu_v1.xlsmfile - Run the
showTestFormmacro from themodShowForms.basmodule
The class includes comprehensive error handling:
- Initialization checks: Validates that context menu names are properly set
- Control validation: Ensures controls exist before manipulation
- Safe cleanup: Properly removes context menus when objects are terminated
- Detailed error messages: Provides specific error descriptions to help troubleshoot issues
-
Enhanced styling options
- Add properties to customize the appearance of the context menu
- Allow custom colors, fonts, and spacing configurations
-
Improved event handling
- Add more granular event handling for menu interactions
- Support for before/after action events
-
Submenu support
- Extend functionality to support nested context menus
- Create hierarchical menu structures
-
Persistent configuration
- Add methods to save and load context menu configurations
- Support for predefined menu templates
-
Performance optimization
- Optimize for scenarios with many context menu items
- Implement lazy loading for complex menu structures
-
Additional control types
- Expand supported control types with checkboxes, radio buttons, etc.
- Add separators and other UI elements
The project is distributed under the Apache License 2.0. See the LICENSE file for details.
VBATools
