Solved

Trapping non-text paste into richtextbox

Posted on 2002-07-30
11
225 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now