AJP Excel Information

Charts













 
 

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 [] (53KB)
 
 
   

Last updated 7th January 2010

 
 
  Home | Charts | VBA Code | Fun Stuff
Newsgroups | Tips | Links | What's New | Book List
Contact | About
Microsoft® and Microsoft® Excel are registered trademarks of the Microsoft Corporation.
andypope.info is not associated with Microsoft. Copyright ©2007-2009 Andy Pope