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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable right click popupmenu of textboxes 6

Status
Not open for further replies.

Crazyf

Programmer
Jun 15, 2007
29
GR
hi guys.
I want to disable right click in textboxes. And that's why because i want to prevent the copy paste data from one textbox to another, i have created validations in textboxes, so every textbox has its own validation and i don't want the user copy paste data for one to another textbox. I have had a look at thread222-1070885, the code that refers there disables copy paste from clipboard and popupmenu appears when the user right cliks on the textbox, but i want something more than this, i want when the user right clicks in a textbox not appears the popupmenu. Any suggestions??
Any help will be much appreciated.

Thank you
in advanced
 
I wouldn't do that, personally. Keep in mind that your users are used to the way that Windows works, and you should only reluctantly attempt to change it. For example, how do you think a user would feel if she were attempting to copy data from one of your text boxes into, say, Notepad and couldn't do it? In other words, I would only suppress things that were absolutely necessary for reasons like security, validation and the like.

Bob
 
What do you mean that you would only suppress things that were been absolutely necessary for reasons like security, validation and the like? Do you thing that is better just to disable copy paste rather than right click? What do you suggest?
 
Also, I am just thinking that if the validation of a textbox is to type only numbers and the user copy paste alphabetical characters it will cause problem. I am waiting your opinion. Thank you very much Bob.
 
<Do you thing that is better just to disable copy paste rather than right click?

See, I wouldn't suppress the Undo and the Select All functions in the popup menu unless it were required for the security of my data. And it doesn't seem that it would be. So, personally, I would suppress the paste as in Hypetia's code, and allow the rest of the popup menu to work. (It would be nice to grey out the Paste selection in the menu, too. Perhaps Hypetia has some code for that?)

HTH

Bob
 
Thank you very much Bob, i find it really idea to grey out the Paste selection in the menu, but i don't know how to do this. I have ran hypetia's code In the thread222-1070885: Disable Paste function, when the user clicks on the disable command button, really the user can not paste data in a textbox, but the paste selection in the menu does not grey out. Any suggestions how to manage this?
Must I have to open another theat about this or not?
 
<Also, I am just thinking that if the validation of a textbox is to type only numbers and the user copy paste alphabetical characters it will cause problem. >

Unless your code is running independent of the users action after these text boxes are populated I do not see the problem here.

If a user can accurately fill these textboxes
why would these same users start pasting inaccurate data.
It seems to me that the validation you mentioned would cover any problems that may occur, unless after the validation you are allowing your users to continue with invalid data entries.
 
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Clipboard.Clear
End Sub

Stops users pasting into the text box even with Ctl + V and greys out the Paste item in the popup menu.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
You need to do validation in the Change event:
Place this in a public module:
Code:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const EM_CANUNDO = &HC6
Public Const EM_UNDO = &HC7
Public Const EM_EMPTYUNDOBUFFER = &HCD
And this in the form with the text box (change the text box Text1 name to that of yours)
Code:
Private Sub Text1_Change()
    Static bCancel  As Boolean
    If bCancel Then Exit Sub
    If (Not IsNumeric(Text1.Text & "E0")) Then
        bCancel = True
        If SendMessage(Text1.hwnd, EM_CANUNDO, 0&, 0&) = 0 Then
            Text1.Text = vbNullString
        Else
            SendMessage Text1.hwnd, EM_UNDO, 0&, 0&
            Text1.SelStart = Len(Text1.Text)
        End If
        bCancel = False
    End If
    SendMessage Text1.hwnd, EM_EMPTYUNDOBUFFER, 0&, 0&
End Sub

I got some bits and pieces of this code from several threads\FAQs in this forum.
 
Not sure if this is the best idea but you could use
the richtextbox control and leave its Autoverb setting at default "False", however some savvy users will still come along and hit ctrl+v and paste away.
 
>Deserves 30 slashes

Is that good or bad? [bigsmile]

[gray]Experience is something you don't get until just after you need it.[/gray]
 
>Is that good or bad?

Well, I always thought 30 slashes were generally considered bad, but maybe not for everyone.
 
Ah, you mean 30 lashes!

In which case, yes I probably do deserve at least 30 but Clipboard.clear certainly achieves the objective. [thumbsup]

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
>lashes
Yes, of course

>certainly achieves the objective

With possiblly a very upset user ...

I would certainly be upset if a programm did that with my clipboard contents (possibly contents of a large Cut from another application), with out explicitly clicking on a Copy/Cut menu function, when maybe I only wanted to right click to use the Undo or Selection functions.
 
I would never clear the clipboard. Clipboard is a shared system resource and clearing it just to solve the purpose is not a decent idea in my opinion.

Similar to the code for disabling paste function, I posted code in thread222-588883 for disabling the context menu.

Instead of completely disabling the context menu, you can display a customized context menu of your own when WM_CONTEXTMENU messasge is sent. This popup menu will mimic the standard text box context menu, having Paste function disabled or missing altogether.

You should combine the above two techniques (disabling paste function and showing a customized context menu). Also, you have to prepare your menu before displaying it and process all commands which user will choose from your menu by sending appropriate messages to the textbox in question.

For example, you have to send EM_CANUNDO message to determine the enable/disable state of Undo command before displaying the menu. Similarly, if the user chooses the Undo command then you have to send WM_UNDO message to undo the last change.

This leaves more work to do on your part.

Unfortunately, I cannot find a reliable way of modifying the default menu of a textbox and disabling/removing the Paste menu item from it.
 

You still would have the problem when using the keyboard to paste (Shift-Ins and Ctrl-V).

Also, numeric validation of the clipboard cóntents can be done (= IsNumeric(clipboard.GetText & "E0"))

I think using the Change Event from above, along with a customized context menu is a good solution which can be fairly easily implemented.
 
Here is 29 lashes

Code:
Dim retainclipboardStr As String


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If retainclipboardStr = "" Then
    Else
        Clipboard.SetText retainclipboardStr
    End If
End Sub

Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Clipboard.GetText = "" Then
    Else
        retainclipboardStr = Clipboard.GetText
        Clipboard.Clear
    End If
End Sub
Pasted here with ctrl-v
 
I had thought of something similar:

Dim retainclipboardStr As String

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
retainclipboardStr = Clipboard.GetText
Clipboard.Clear
End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Clipboard.SetText retainclipboardStr
End Sub

But of course this only works with text.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Hypetia the what i wanted to do firstly, was what exactly you have done in thread222-588883: Right click in a textbox
in your last code. I wanted to disable right click so the user couldn't cut-copy-paste whatever from one textbox to another and that's because as i mentioned before, i have a kind of validation in several text boxes and i wanted anyway to prevent this, because if with i copy or cut paste data to from one to another textboxes it would be a problem for exampble if a textbox accepts only numbers for some calculations. Also, i combined your code and SBerthold code so the user cannot paste data either from keyboard either with righ click on it. Although, I have some questions to do. Must I have to disable paste selection if i have disabled textboxe's right click? Do i have to create a customized context menu as you told me before?
And my last question is what for three57m and SBerthold.
What exactly are lashes?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top