Skip to content

vbatools/UserForms-Class-Context-Menu-VBA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clsContextMenu - Context Menu Class for VBA UserForms

English | Русский | UserForms-Class-ALL

Description

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.

Project Demo

Features

  • 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

Requirements

  • Microsoft Excel with VBA support
  • Windows operating system for CommandBar functionality

Installation

  1. Copy the clsContextMenu.cls file to your VBA project
  2. Import the class in the VBA editor

Usage

Basic Usage

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")

Advanced Usage

' 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")

Binding to Multiple Controls

' 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

Properties

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

Methods

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

Enums

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

Examples

Usage example is presented in the frmTestClass.frm form, which demonstrates all class capabilities.

To run the example:

  1. Open the context_menu_v1.xlsm file
  2. Run the showTestForm macro from the modShowForms.bas module

Error Handling

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

Recommendations for Improvement

  1. Enhanced styling options

    • Add properties to customize the appearance of the context menu
    • Allow custom colors, fonts, and spacing configurations
  2. Improved event handling

    • Add more granular event handling for menu interactions
    • Support for before/after action events
  3. Submenu support

    • Extend functionality to support nested context menus
    • Create hierarchical menu structures
  4. Persistent configuration

    • Add methods to save and load context menu configurations
    • Support for predefined menu templates
  5. Performance optimization

    • Optimize for scenarios with many context menu items
    • Implement lazy loading for complex menu structures
  6. Additional control types

    • Expand supported control types with checkboxes, radio buttons, etc.
    • Add separators and other UI elements

License

The project is distributed under the Apache License 2.0. See the LICENSE file for details.

Author

VBATools

About

A VBA class library for creating customizable context menus in Excel UserForms.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages