Suppressing the Paste Operation
Hi Guys
I want to control and trap the Paste (Ctrl + V) operation in a text box.
Now, in the KeyDown event I am able to trap the keys for Control and V, and
suppress them. But the system's paste operation follows the KeyDown event.
Eventually, I end up having the text from clipboard pasted onto text box.
In shrot, is there a way to suppress the Paste operation in a textbox.
Thanx
Re: Suppressing the Paste Operation
Sunil -
You can subclass the textbox and trap the WM_PASTE message. If you
only want to disable the (Ctrl V) paste then you can set a flag in the
KeyDown event and reset it in the KeyUp event. This will still allow a
paste
using the popup menu. If you want to disable all pasting then don't use any
flags.
Jim Edgar
"Sunil Budhrani" <SunilBudhrani@Yahoo.com> wrote in message
news:396689e8$1@news.devx.com...
>
> Hi Guys
>
> I want to control and trap the Paste (Ctrl + V) operation in a text box.
> Now, in the KeyDown event I am able to trap the keys for Control and V,
and
> suppress them. But the system's paste operation follows the KeyDown event.
> Eventually, I end up having the text from clipboard pasted onto text box.
>
> In shrot, is there a way to suppress the Paste operation in a textbox.
>
> Thanx
Re: Suppressing the Paste Operation
Sunil -
You can subclass the textbox and trap the WM_PASTE message. If you
only want to disable the (Ctrl V) paste then you can set a flag in the
KeyDown event and reset it in the KeyUp event. This will still allow a
paste
using the popup menu. If you want to disable all pasting then don't use any
flags.
Jim Edgar
"Sunil Budhrani" <SunilBudhrani@Yahoo.com> wrote in message
news:396689e8$1@news.devx.com...
>
> Hi Guys
>
> I want to control and trap the Paste (Ctrl + V) operation in a text box.
> Now, in the KeyDown event I am able to trap the keys for Control and V,
and
> suppress them. But the system's paste operation follows the KeyDown event.
> Eventually, I end up having the text from clipboard pasted onto text box.
>
> In shrot, is there a way to suppress the Paste operation in a textbox.
>
> Thanx
Re: Suppressing the Paste Operation
Hi Sunil --
This is a response to the private e-mail you sent to me. The easiest way
to subclass a control is to download a free subclassing control from the
web. I use one from www.softcircuits.com but there are others out there.
Once you download the subclass control start a new project and add a
reference to the control. Put a textbox on the form (using the default
names
of Form1 and Text1) and then put a subclass control on the form. Name
the subclass control subCl. Paste the following code into the form and
run the project. Try pasting into the textbox with Control V and by right-
clicking on the textbox and selecting paste from the menu. One word of
caution: Don't stop the program with the stop button (located on the
toolbar)
or you'll crash the program. Hope it helps...
Jim Edgar
' Begin Cut and Paste
Option Explicit
Const WM_COPY = &H301
Const WM_PASTE = &H302
Private bCntlV As Boolean
Private Sub Form_Load()
' Tell the subclass control what control you
' want to subclass.
subCl.hWnd = Text1.hWnd
' Tell the subclass control what message(s)
' you are looking for. This is an array of
' messages so you can monitor several
' messages. We'll look for the Paste and
' the Copy messages.
subCl.Messages(WM_COPY) = True
subCl.Messages(WM_PASTE) = True
bCntlV = False
End Sub
Private Sub subCl_WndProc(Msg As Long, wParam As Long, _
lParam As Long, Result As
Long)
' This is where you check for the WM_PASTE
' message. If the Text1 control receives
' a paste message then you can intercept it
' here and not allow it to get to the control.
' If you want to stop all pasting into the
' textbox then don't use a flag.
If Msg = WM_COPY Then
Debug.Print "Copy Message Detected"
Else
Debug.Print "Paste Message Detected";
If bCntlV Then ' Control V flag is true
' Do nothing here because we don't want the
' message to get to the textbox
Debug.Print " Control V Flag = True"
Else ' Other type of paste operation
' Send the message to the control
Call subCl.CallWndProc(Msg, wParam, lParam)
Debug.Print " Control V Flag = False"
End If
End If
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
' If the user pressed Cntl V then set the flag to true
bCntlV = (((Shift And vbCtrlMask) > 0) And (KeyCode = vbKeyV))
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
' Set the flag to false
bCntlV = False
End Sub
' End Cut and Paste
Re: Suppressing the Paste Operation
Hi Sunil --
This is a response to the private e-mail you sent to me. The easiest way
to subclass a control is to download a free subclassing control from the
web. I use one from www.softcircuits.com but there are others out there.
Once you download the subclass control start a new project and add a
reference to the control. Put a textbox on the form (using the default
names
of Form1 and Text1) and then put a subclass control on the form. Name
the subclass control subCl. Paste the following code into the form and
run the project. Try pasting into the textbox with Control V and by right-
clicking on the textbox and selecting paste from the menu. One word of
caution: Don't stop the program with the stop button (located on the
toolbar)
or you'll crash the program. Hope it helps...
Jim Edgar
' Begin Cut and Paste
Option Explicit
Const WM_COPY = &H301
Const WM_PASTE = &H302
Private bCntlV As Boolean
Private Sub Form_Load()
' Tell the subclass control what control you
' want to subclass.
subCl.hWnd = Text1.hWnd
' Tell the subclass control what message(s)
' you are looking for. This is an array of
' messages so you can monitor several
' messages. We'll look for the Paste and
' the Copy messages.
subCl.Messages(WM_COPY) = True
subCl.Messages(WM_PASTE) = True
bCntlV = False
End Sub
Private Sub subCl_WndProc(Msg As Long, wParam As Long, _
lParam As Long, Result As
Long)
' This is where you check for the WM_PASTE
' message. If the Text1 control receives
' a paste message then you can intercept it
' here and not allow it to get to the control.
' If you want to stop all pasting into the
' textbox then don't use a flag.
If Msg = WM_COPY Then
Debug.Print "Copy Message Detected"
Else
Debug.Print "Paste Message Detected";
If bCntlV Then ' Control V flag is true
' Do nothing here because we don't want the
' message to get to the textbox
Debug.Print " Control V Flag = True"
Else ' Other type of paste operation
' Send the message to the control
Call subCl.CallWndProc(Msg, wParam, lParam)
Debug.Print " Control V Flag = False"
End If
End If
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
' If the user pressed Cntl V then set the flag to true
bCntlV = (((Shift And vbCtrlMask) > 0) And (KeyCode = vbKeyV))
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
' Set the flag to false
bCntlV = False
End Sub
' End Cut and Paste