1

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 https://gist.github.com/2366507 . 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 https://gist.github.com/2366526) 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
ProcessAllLines()
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

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208

1 Answers1

2

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
        ProcessAllLines
    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
      ProcessAllLines
   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: http://msdn.microsoft.com/en-us/library/windows/desktop/hh768385(v=vs.85).aspx.

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Hello, thank you very much for your answer. I was looking for something like the first code snippet you gave me. I converted it to VB: https://gist.github.com/2367050 and pasted it right under the OnTextChanged Sub, however it doesn't seem to work. Did I do it wrong? P.S. I also tried the second solution (didn't seem to work either). Thanks for your time. Helpful answer however! –  Apr 12 '12 at 12:59
  • @RafalChmiel you welcome but it's not a very good solution, it won't detect if user will paste some text using the context menu or via drag & drop! 2nd solution is a little bit better. – Adriano Repetti Apr 12 '12 at 13:00
  • I don't really care (yet) about the context menu or drag n drop. It's not enabled anyway (context menu) - not sure about dragndrop thou. –  Apr 12 '12 at 13:03
  • @RafalChmiel I guess that code may horrify many people but we'll keep the secret! ;) – Adriano Repetti Apr 12 '12 at 13:13