How to detect multiline paste in RichTextBox


At the moment I’m working on a simple syntax highlighter and I have couple of problems. Could you help me out?

I have a class library with a component class in it. Everything is in VB.NET. It’s only one file so you can see it here . On line 92, there is the OnTextChanged Sub. I was thinking about adding ProcessAllLines() (as on line 128) to the end of that Sub, and it worked. However when I was typing in code to the RichTextBox (source which I used is here after each text change it was checking and processing ALL the lines. So I deleted ProcessAllLines() in the OnTextChanged Sub.

I’m thinking about running ProcessAllLines() when the user pastes something to the SyntaksRichTextBox. However I do not know how to do that. I know that it should be something like:

If [CTRL Pressed] And [V Pressed] Then
End If

I also want it to be in the class library, not the application (Syntaks Demo). Could you help me out here? Thanks.

Rafal Chmiel, @RafalChmiel


Naïve detection

First solution you may think about is to detect the WM_PASTE message overriding the WndProc but unfortunately the control doesn’t send that message to itself when it performs a paste operation.

Detecting keyboard events (you have to override the OnKeyDown function) and checking if the key combinations (CTRL+V and SHIFT+INS) is the one to paste text you may solve this.

Protected Sub OnKeyDown(ByVal e As KeyEventArgs)
    Dim ctrlV As Boolean = e.Modifiers = Keys.Control && e.KeyCode = Keys.V
    Dim shiftIns As Boolean = e.Modifiers = Keys.Shift && e.KeyCode = Keys.Insert

    If ctrlV Or shiftIns Then
    End If
End Sub

It works well but you can’t catch the paste operation made using the mouse (right click to open the context menu) and the paste operations made via drag & drop. If you do not need them you can use this solution (at least it’s simply and straightforward).

Better detection

Assumption: when user types inside the RichTextBox he inserts one character per time. How can you use this? Well, when you detect a bigger change you detected a paste operation because user can’t type more than once character per time (this isn’t always true because of Unicode surrogates but it’s not a problem in this case).

It doesn’t work well with every IME (I didn’t try with far east languages, for example) and with Unicode surrogates but for western languages it’s OK (anyway even when it doesn’t work you’ll simply reprocess all lines). Also read this post and this post for more details about Unicode and this (twin) answer for C# version.

Dim _previousLength As Int32 = 0

Protected Sub richTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
   Dim currentLength As Int32 = richTextBox.Text.Length
   If Math.Abs(currentLength - _previousLength) > 1 Then
   End If

   _previousLength = currentLength
End Sub

Perfect solution

The perfect solution of course exists (on Windows 8 or higher), the native rich edit control sends a EN_CLIPFORMAT notification message. It’s intended to notify a rich edit control’s parent window that a paste occurred with a particular clipboard format. You can then override the WndProc of its parent to detect the WM_NOTIFY message for this notification. Anyway it’s not few lines of code, check this link on MSDN for details:

Answered By – Adriano Repetti

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.