Link to home
Start Free TrialLog in
Avatar of RichBisset
RichBisset

asked on

Trapping non-text paste into richtextbox

I have a richtextbox which i dont want users to be able to paste anything but text into (ie. files, images etc..) i have the following code which detects the paste action within my textbox but whatever i try to do it still pastes the file into it.

if i try pasting a piece of text then the relevant msgbox appears but if i paste an object such as a file it doesnt display a msgbox and pastes the file into the box.

==================================================================
Private Sub Form_KeyUp(KeyAscii As Integer, Shift As Integer)
If KeyAscii = (vbKeyV And Shift = 2) Then
    If Clipboard.GetFormat(1) <> True Then
        MsgBox ("YOU ARE PASTING A FILE")
        Clipboard.Clear
        KeyAscii = 0
    Else
        MsgBox ("YOU ARE PASTING TEXT")
    End If
End If

End Sub
==================================================================

i have disabled OLEdrag. I am only using a richtextbox so i can colour certain pieces of text red
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

I am assuming the Form KeyPreview property is true. Than the problem you are encountering happens when the RichTextBox does not have the focus.

Try detecting in the KeyPress event as opposed to the KeyUp event (as an aside the parameters in the KeyUp event are KeyCode and Shift, not KeyAscii and Shift), you also may find it simpler to read:

Change your code as follows:

Private Sub Form_KeyPress(KeyAscii As Integer)

If KeyAscii = 22 Then ' User pressed Ctrl-V
   If Clipboard.GetFormat(1) <> True Then
       MsgBox ("YOU ARE PASTING A FILE")
       Clipboard.Clear
       KeyAscii = 0
   Else
       MsgBox ("YOU ARE PASTING TEXT")
   End If
End If

End Sub

Anthony
Avatar of RichBisset
RichBisset

ASKER

i tried out your suggestions Anthony and i now have the form's KeyPreview property set to true and have moved the code to the KeyPress event instead.

However, now it lets me paste the file into the RTB once only, every subsequent time it just displays the message box and doesnt paste.
Post your complete code where you do your paste.

Anthony

Private Sub Form_KeyPress(KeyCode As Integer)

If KeyCode = 22 Then ' User pressed Ctrl-V
  If Clipboard.GetFormat(1) <> True Then
      Clipboard.Clear
      KeyCode = 0
      txtMsgContent.SetFocus
      MsgBox ("YOU ARE PASTING A FILE")
  Else
      MsgBox ("YOU ARE PASTING TEXT")
      txtMsgContent.SetFocus
  End If
End If

End Sub
I was only assuming that the Form KeyPreview property was set to true as you were using the Form_KeyPress, I was not recommending it.  Here is a better solution that does not rely on this property being set to True.  The caveat is that in order to paste the text to the Rich Text Box it must have the focus.

1. Start a new project.
2. Add the Microsoft Rich TextBox (RichTextBox1) to your form.
3. Add the following code (notice that the correct paramter name for the _KeyPress events is "KeyAscii"):

Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)

If KeyAscii = 22 Then ' User pressed Ctrl-V
   If Clipboard.GetFormat(1) <> True Then
      Clipboard.Clear
      KeyAscii = 0
      MsgBox "YOU ARE PASTING A FILE"
   Else
      MsgBox "YOU ARE PASTING TEXT"
   End If
End If

End Sub

Anthony
I tried out the above solution but it still pastes the file the first time and then every subsequent time it doesnt.

It does display the msgbox in both cases however.
Try commenting out the Clipboard.Clear line, and you may find it more succesful the second time around.  As in:

If KeyAscii = 22 Then ' User pressed Ctrl-V
  If Clipboard.GetFormat(1) <> True Then
     ' Clipboard.Clear             '<-- Comment this line
     KeyAscii = 0
     MsgBox "YOU ARE PASTING A FILE"
  Else
     MsgBox "YOU ARE PASTING TEXT"
  End If
End If

End Sub

Anthony
I believe I misunderstood you.  Remove the KeyPress event and try it this way (not unlike your original plan, except I am using KeyDown event for the RichTextBox, rather than the KeyUp event for the Form)

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyV And (Shift And vbCtrlMask) > 0 Then
  If Clipboard.GetFormat(vbCFText) Then
       MsgBox "YOU ARE PASTING TEXT"
  Else
     Clipboard.Clear
     KeyCode = 0
     MsgBox "YOU ARE PASTING something else"
  End If
End If

End Sub

Anthony
Thats seems to have done the trick. however when i paste a file in to the field it doesnt paste the file but it still outputs the 'v' of CTRL + V.

This is a strange as, when i paste text it doesnt paste an extra 'v'.

Any ideas?



Private Sub txtMsgContent_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyV And (Shift And vbCtrlMask) > 0 Then
    If Clipboard.GetFormat(vbCFText) Then
    Else
        MsgBox "You can only paste textual data into this field", vbOKOnly + vbInformation, "Warning"
    End If
End If

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick.

Many thanks Anthony