Visual Basic > Forms
Displaying a custom right click popup menu in a textbox
Displaying a custom right click popup menu in a textbox The following code demonstrates how to display a custom right click popup menu in a VB textbox without using subclassing. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long 'Purpose : Shows a custom right click menu for a textbox 'Inputs : Button The button pressed by the user ' txtControl The textbox to display the new popup menu in. ' oPopMenu The popup menu to display 'Outputs : Returns True if a popup menu was displayed 'Notes : Prevents the default content menu from begining displayed. Function TextBoxRightClickMenu(Button As Integer, txtControl As TextBox, oPopMenu As Object) As Boolean On Error GoTo ErrFailed If Button = vbRightButton Then 'Lock the window updating to prevent the control being displayed as disabled LockWindowUpdate txtControl.hWnd 'Disable the control txtControl.Enabled = False 'Show the popup PopupMenu oPopMenu 'Enable the control txtControl.Enabled = False 'Restore the window updating LockWindowUpdate 0& 'Return success TextBoxRightClickMenu = True End If Exit Function ErrFailed: Debug.Print "Error in TextBoxRightClickMenu: " & Err.Description TextBoxRightClickMenu = False End Function 'Demonstration routine, where MnuPop is a menu containing at least on visible sub menu. Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) TextBoxRightClickMenu Button, Text1, MnuPop End Sub
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows