AJP Excel Information AJP Excel Information

Userform Contextual Menu class code

 
 

 
The class module handles the construction of the contextual menu, the capture of right clicking in textboxes and the actual Cut. Copy and Paste actions. The class makes use of the userform's ActiveControl object. The code even handles controls within container controls such as Frames and Multipage.

The follow Initialization code, from the userform, shows how simple it is to define and use the class object. You only need declare a variable to the object and then set a reference for each textbox you want to have contextual menu capabilities.

Private m_colContextMenus As Collection

Private Sub UserForm_Initialize()

    Dim clsContextMenu As CTextBox_ContextMenu

    Set m_colContextMenus = New Collection

    Set clsContextMenu = New CTextBox_ContextMenu
    With clsContextMenu
        Set .TBox = UserForm1.TextBox1
        Set .Parent = Me
    End With
    m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)

    Set clsContextMenu = New CTextBox_ContextMenu
    With clsContextMenu
        Set .TBox = UserForm1.TextBox2
        Set .Parent = Me
    End With
    m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)

    Set clsContextMenu = New CTextBox_ContextMenu
    With clsContextMenu
        Set .TBox = UserForm1.TextBox3
        Set .Parent = Me
    End With
    m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)
    
    Set clsContextMenu = New CTextBox_ContextMenu
    With clsContextMenu
        Set .TBox = UserForm1.TextBox4
        Set .Parent = Me
    End With
    m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)
        
    Set clsContextMenu = New CTextBox_ContextMenu
    With clsContextMenu
        Set .TBox = UserForm1.TextBox5
        Set .Parent = Me
    End With
    m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)
        
End Sub
 

 
 
   

Download example workbook which contains both .xls and .xlsm files

Created August 2004
Last updated 5th August 2014 


Return to main page Chart Section VBA section Fun and games section Forum files Tips section Links section Book section Site information Site Search RSS feed Top of page


Microsoft® and Microsoft® Excel are registered trademarks of the Microsoft Corporation.
andypope.info is not associated with Microsoft. Copyright ©2007-2014 Andy Pope