Solved

Trapping non-text paste into richtextbox

Posted on 2002-07-30
11
228 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:RichBisset
  • 6
  • 5
11 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7189334
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
0
 

Author Comment

by:RichBisset
ID: 7190175
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7190628
Post your complete code where you do your paste.

Anthony
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:RichBisset
ID: 7190922

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
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7192169
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
0
 

Author Comment

by:RichBisset
ID: 7192757
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7193532
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
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7193727
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
0
 

Author Comment

by:RichBisset
ID: 7193889
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
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 25 total points
ID: 7193928
I suspect you overlooked setting the KeyCode to 0.  Try:

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
       KeyCode = 0       '<-- Add this
       MsgBox "You can only paste textual data into this field", vbOKOnly + vbInformation, "Warning"
   End If
End If

End Sub

Anthony
0
 

Author Comment

by:RichBisset
ID: 7195414
That did the trick.

Many thanks Anthony
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question