Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right click in a textbox 1

Status
Not open for further replies.

dalec

Programmer
Jul 8, 2000
191
US
Hi, I have an application with a textbox when the program is running and a you select text in the textbox, then right click a popup menu appears with the usual cut, copy past, select all.

My question is how do you disable this from happening, I don't want users being able to cut and past text that is displayed in the applicaton.

thanks in advance,
Dale
 
Quick guess but I came across a reference to this dialogue in Help/MSDN and it talked about adding to it - I would suspect it has properties that would be changable.

Another idea is to use the object_MouseDown event to trap all clicks and deal with them as you wish. I do this on a form and now I think about it I haven't been getting that dialogue appear. I will go and investigate.
 
I just created a menu item mnuFile (=invisible) with one sub menu that has no code (as said to be required). It does not show at run time

then in my text box words

Private Sub words_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then PopupMenu mnuFile
End Sub
Private Sub words_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then PopupMenu mnuFile
End Sub

the mouseup event saved a load if ideosycratic behaviour which seemd to dump the clicks into the form that held the object which I have coded to respond.

It is clunky but it prevents the default menu from appearing.

Now to see if I can use this knowledge........
 
Simple. Have a textbox(Text1) on your form with the following code.
___
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then SetCapture hwnd
End Sub
 
Hypetia,>>
but that dusnt work completely. Try right double clicking on the textbox...
 
You can take care of this as well with this code.
___

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then SetCapture hwnd
End Sub
Private Sub Text1_DblClick()
If GetAsyncKeyState(vbRightButton) < 0 Then SetCapture hwnd
End Sub
___

Now you'll ask how to prevent this menu from poping up if the user presses the menu key or Shift+F10.
 
Well, I figured that out. Here is an alternate approach to completely disable the context menu, without any flaw.

Here I have used subclassing. So don't terminate your program using End statement or End button on toolbar.

Add a bas module and paste the following code in it.
___

Declare Function SetWindowLong& Lib &quot;user32&quot; Alias &quot;SetWindowLongA&quot; (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
Declare Function CallWindowProc& Lib &quot;user32&quot; Alias &quot;CallWindowProcA&quot; (ByVal lpPrevWndFunc&, ByVal hwnd&, ByVal uMsg&, ByVal wParam&, ByVal lParam&)
Const GWL_WNDPROC = (-4)
Dim lppwp& '(Long) Pointer to Previous Window Procedure
Function WindowProc&(ByVal hwnd&, ByVal uMsg&, ByVal wParam&, ByVal lParam&)
If uMsg = &H7B& Then uMsg = 0 'Disable WM_CONTEXTMENU
WindowProc = CallWindowProc(lppwp, hwnd, uMsg, wParam, lParam)
End Function
Sub SetContextMenu(Txt As TextBox, bEnable As Boolean)
If bEnable Then
If lppwp Then SetWindowLong Txt.hwnd, GWL_WNDPROC, lppwp
Else
Dim Ret As Long
Ret = SetWindowLong(Txt.hwnd, GWL_WNDPROC, AddressOf WindowProc)
If lppwp = 0 Then lppwp = Ret
End If
End Sub
___

And put the following code in your form. (containing the textbox Text1)
___

Private Sub Form_Load()
SetContextMenu Text1, False
End Sub
___

Test the program.

You can disable the menu on more than one textboxes by calling the SetContextMenu procedure with each textbox.

You can also enable the context menu later by calling this procedure again with bEnable parameter set to True.
 
Thank you very much, that helped greatly!!!
 
Good Effort Hypetia,

Am doing the &quot;starring&quot; for dalec. Just to express my appreciation for a dedicated effort to solve Dalec's issue.

Tek-Tips Rulz..
 
I will have a closer look at API route when I get home. Thanks Hypetia.
 
I used the code Hypetia provided. It appears it works with compiled executables. It however, produces the fatal memory error in design window each time when I stop the project.
I use VB6 and Windows NT.

I am wondering if anybody else experiences this problem.
 
Hypetia did warn against this. You must unhook before quitting the program in the IDE, otherwise you can expect exactly what you have experienced
 
I repeat it again, you should not stop the program using the the End button on the toolbar. Always stop the program using the Unload method in the code or by clicking the Close button on the titlebar.
 
Thank you for the reply. I actually, found another approach that prevents the user from pasting into the field:

1. Don't let typing Cntrl^V and Shft^Ins in Key_Down, Key_Press.
2. I use Locked and BackColor properties. If I lock the field I will make it grey (unlock - white).
3. I Put the following code in mouse events:

Private Sub txtCalc_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
txtCalc(Index).Locked = Button = vbRightButton
End Sub

Private Sub txtCalc_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
txtCalc(Index).Locked = txtCalc(Index).BackColor = COLOR_BACK_LOCKED
End Sub

If the field was permanently accessable then in MouseUp I could set Locked to False unconditionally.

It works for me just fine.

Thank you again

Vlad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top