How to prevent copy and paste richtext to richtextbox?

I am using richtextbox for the user to enter plaintext.
The reason I am using richtextbox is because the scrollbar of richtextbox is automatically appear and disappear depends on the length of word, but textbox's scrollbar is always there.
But the problem is, the user still can copy and paste the formatted text(Bold, italic, etc) to the richtextbox. Is there a way to make all the copied formatted text paste as plain text, or any other mean in order to make the richtextbox not to show the richtext?
LVL 9
william007Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
GrahamSkanConnect With a Mentor RetiredCommented:
The text is an RTF box is always formatted, but if all the text has the same font characteristics, you won't notice it.

This will help to keep the formatting consistent. The list of properies is not exhaustive, however and you may need to add more.

Private Sub RichTextBox1_Change()
    Dim s As Long
    Dim l As Long
    Dim l2 As Long
    s = RichTextBox1.SelStart
    l = RichTextBox1.SelLength
    l2 = Len(RichTextBox1.Text)
    RichTextBox1.SelStart = 0
    RichTextBox1.SelLength = l2
       With RichTextBox1
          .SelFontName = "MS Sans Serif"
          .SelFontSize = 8
          .SelBold = False
          .SelItalic = False
          .SelStrikeThru = False
          .SelUnderline = False
       End With
    RichTextBox1.SelStart = s
    RichTextBox1.SelLength = l
End Sub
0
 
Anthony2000Commented:
If you want to disable pasting to the RichTextBox both from using the RichTextBox's context menu and the keyboard:

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
   Debug.Print KeyCode ' so I could see what code ctrl-v produced, I found it to be 86
   If KeyCode = 86 Then
      KeyCode = 0 ' cancel the ctrl-v (paste)
   End If
End Sub

as for right-mouse content menu, I modified the RichTextBox's
AutoVerbMenu and set it to False

Otherwise, you can trap on the WM_PASTE message using a subclassing technique (see http://support.microsoft.com/kb/q179398/). This maybe more complicated than you want.

Anthony


Anthony

0
 
william007Author Commented:
Thanks:-)
0
All Courses

From novice to tech pro — start learning today.