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

Disable Paste function

Status
Not open for further replies.

xinyin

Programmer
Jan 16, 2003
81
HK
Is it possible to disable the paste function in a control? That is, the user is not allowed to press Ctrl + V, or Shift + Insert, or right click mouse and then choose "Paste" to insert any thing into the control. They can only input by typing.
 
One way would be to clear the clipboard using
Code:
Clipboard.Clear
on the MouseDown and KeyDown events of the textbox.

Hope this helps

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
You can do this by subclassing the control and suppressing the WM_PASTE message which is sent to a control for performing a paste operation.

See the following code.

Place two command buttons and a text box on the form and insert the following code.
___
[tt]
Private Sub Form_Load()
Command1.Caption = "Enable paste"
Command2.Caption = "Disable paste"
End Sub

Private Sub Command1_Click()
EnablePaste Text1.hWnd
End Sub

Private Sub Command2_Click()
DisablePaste Text1.hWnd
End Sub[/tt]
___

Now place the following subclassing code in a module and test the program.
___
[tt]
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GWL_WNDPROC = (-4)
Const WM_PASTE = &H302
Dim lppwp As Long 'Pointer to Previous Window Procedure
Private Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_PASTE Then uMsg = 0
WindowProc = CallWindowProc(lppwp, hWnd, uMsg, wParam, lParam)
End Function
Sub EnablePaste(hWnd As Long)
If lppwp Then SetWindowLong hWnd, GWL_WNDPROC, lppwp
End Sub
Sub DisablePaste(hWnd As Long)
Dim Ret As Long
Ret = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
If lppwp = 0 Then lppwp = Ret
End Sub[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top