[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Trapping non-text paste into richtextbox

Posted on 2002-07-30
11
Medium Priority
?
234 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

590 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