HOWTO: Suppress Default Pop-up Menu When Using Custom MenuID: Q191670
|
Some Visual Basic controls, such as the TextBox, have a default pop-up menu that will automatically be display when you alternate-mouse click on the control. This article demonstrates one way to disable this default pop-up menu in order that either no menu or only a custom pop-up menu is displayed.
When you alternate-mouse click on the TextBox control, its default pop-up
menu will be displayed. Visual Basic does not have a property or any other
built-in mechanism that directly disables this feature. However, setting
the control's Enabled property to False will prevent the menu from being
displayed although this allows the user to see that the control is
disabled.
One workaround is to use the Windows LockWindowUpdate API in conjunction
with the Enabled property. The LockWindowUpdate function disables or re-
enables drawing in a specified window. After the operation is complete, the
control is re-enabled and the LockWindowUpdate API is called a second time
to resume drawing of the control.
File
New
Open
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hwndLock As Long) As Long
Private Sub mnuOne_Click()
Text1.Text = "Menu One was clicked"
End Sub
Private Sub mnuTwo_Click()
Text1.Text = "Menu two was clicked"
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = vbRightButton Then
' Avoid the 'disabled' gray text by locking updates
LockWindowUpdate Text1.hWnd
' A disabled TextBox will not display a context menu
Text1.Enabled = False
' Give the previous line time to complete
DoEvents
' Display our own context menu
PopupMenu mnuPopup
' Enable the control again
Text1.Enabled = True
' Unlock updates
LockWindowUpdate 0&
End If
End Sub
For additional information, please see the following article in the
Microsoft Knowledge Base:
Q170570 : HOWTO: Build a Windows Message Handler with AddressOf in VB
Q155969 : HOWTO: Distribute the WebBrowser Control
Additional query words:
kbDSupport kbDSD kbVBp600 kbInternet kbVBp kbVBp400 kbVBp500 kbAPI kbVBp
kbSDKWin32 kbMenu
Keywords :
Version :
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 15, 1999