is there a way to disable the right click on a text box?
Printable View
is there a way to disable the right click on a text box?
[code]
'get rid of popup for text box on right click
'
'<<<<<<<<<< put this in a bas module >>>>>>>>>>>>>>>>>>
'
Option Explicit
Public Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public lpPrevWndProc As Long
Public lngHWnd As Long
Public Sub Hook(hWnd As Long)
lngHWnd = hWnd
lpPrevWndProc = SetWindowLong(lngHWnd, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub
Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(lngHWnd, GWL_WNDPROC, _
lpPrevWndProc)
End Sub
Public Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Select Case uMsg
Case WM_RBUTTONUP
'Do nothing
'Or popup your own menu
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hw, _
uMsg, wParam, lParam)
End Select
End Function
' <<<<<<<<<<<<<<<< form code >>>>>>>>>>>>>>>>>>>>>
'Add the following code to the Form_Load event of the form where the text box is placed:
Call Hook(Text1.hWnd)
'Where Text1 is the name of the text box you want to Subclass.
'Add the following code to the Form_Unload event:
Call UnHook
[code]
Add the following to a Module.
Add the following to a Form with a TextBox.Code:Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GWL_WNDPROC = (-4)
Const WM_RBUTTONDOWN = &H204
Private WndProcOld As Long
Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_RBUTTONDOWN Then Exit Function
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub
Code:Private Sub Form_Load()
SubClassWnd Text1.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd Text1.hwnd
End Sub
Thanks guys, Megatron, how did you get the Text to come up blue? or is it just another color coding mistake at the vbworld forunms?
HeSaidJoe,
Your code is good. But can you teach me if I have multiple text box (I don't know the exact number, they are in an array), how should I modify the source.
Hi,
Try easy one.
Steps to Create Sample Project
Start a new Standard EXE project in Visual Basic. Form1 is created by default.
Add a TextBox control to Form1.
Choose Menu Editor from the Tools menu and create a menu named mnuPopUp on Form1. Deselect the Visible CheckBox and add items such as the following:
File
New
Open
Add the following code to the code window of Form1:
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
Save and run the project.
Alternate-mouse click on Text1. Only the custom menu is displayed. The standard editing menu is not shown.
Looks like the final code tag in HesaidJoes post triggered the blue text in Megatrons post