How to prevent copy and paste richtext to richtextbox?

Posted on 2006-04-13
Last Modified: 2008-01-16
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?
Question by:william007
    LVL 76

    Accepted Solution

    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
    LVL 8

    Expert Comment

    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 This maybe more complicated than you want.



    LVL 9

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    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…
    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…

    758 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

    12 Experts available now in Live!

    Get 1:1 Help Now