?
Solved

Trapping non-text paste into richtextbox

Posted on 2002-07-30
11
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 100 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

771 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