RichTextBox syntax highlighting in real time--Disabling the repaint
It is an oversight in the RichTextBox class. Other controls, like ListBox, support the BeginUpdate and EndUpdate methods to suppress painting. Those methods generate the WM_SETREDRAW message. RTB in fact supports this message, but they forgot to add the methods.
Just add them yourself. Project + Add Class, paste the code shown below. Compile and drop the control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class MyRichTextBox : RichTextBox {
public void BeginUpdate() {
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
}
public void EndUpdate() {
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
this.Invalidate();
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int WM_SETREDRAW = 0x0b;
}
Or P/Invoke SendMessage directly before/after you update the text.
Real time syntax highlighting in a RichTextBox
I decided to try something and it worked amazingly!
I'm highlighting one line at a time. So when the keyUp gets triggered I only parse the selected line. So NO flickering!
And at the startup I made a HighlightLines() method that loop through the lines and call my HighlightLine(lineIndex) method.
I'll try to mix my solution with Cheeso's and I guess it'll make something awesome!
Thanks
RichTextBox Syntax highlighting out of main thread best practice?
It is slower because invoking is expensive. It requires two thread context switches and the UI thread has to be idle. Do this for every single highlighting change and you'll seriously bog down the thread. Using BeginInvoke() would fix that, but now you'll bog down the UI thread so it gets unresponsive. In other words, you can't win with that strategy.
Not sure what's going wrong with the helper RTB. It is off by two for each line, smells like a simple bug. Like not accounting for a carriage return + line feed at the end of the line.
A better way to speed this up is to tell the RTB to not update itself when you're busy highlighting text. Very big difference. That's not built-in but you can easily add it. And consider cutting your losses, RTB is just not a very good editor. Look at ScintillaNET.
C# how to stop RichTextBox redraw?
Decision found and it's working.
Wrote a wrap-class using this code
Class itself:
public class RichTextBoxRedrawHandler
{
RichTextBox rtb;
public RichTextBoxRedrawHandler (RichTextBox _rtb)
{
rtb = _rtb;
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, ref Point lParam);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, IntPtr lParam);
const int WM_USER = 1024;
const int WM_SETREDRAW = 11;
const int EM_GETEVENTMASK = WM_USER + 59;
const int EM_SETEVENTMASK = WM_USER + 69;
const int EM_GETSCROLLPOS = WM_USER + 221;
const int EM_SETSCROLLPOS = WM_USER + 222;
private Point _ScrollPoint;
private bool _Painting = true;
private IntPtr _EventMask;
private int _SuspendIndex = 0;
private int _SuspendLength = 0;
public void SuspendPainting()
{
if (_Painting)
{
_SuspendIndex = rtb.SelectionStart;
_SuspendLength = rtb.SelectionLength;
SendMessage(rtb.Handle, EM_GETSCROLLPOS, 0, ref _ScrollPoint);
SendMessage(rtb.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
_EventMask = SendMessage(rtb.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
_Painting = false;
}
}
public void ResumePainting()
{
if (!_Painting)
{
rtb.Select(_SuspendIndex, _SuspendLength);
SendMessage(rtb.Handle, EM_SETSCROLLPOS, 0, ref _ScrollPoint);
SendMessage(rtb.Handle, EM_SETEVENTMASK, 0, _EventMask);
SendMessage(rtb.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
_Painting = true;
rtb.Invalidate();
}
}
}
Usage:
RichTextBoxRedrawHandler rh = new RichTextBoxRedrawHandler(richTextBoxActually);
rh.SuspendPainting();
// do things with richTextBox
rh.ResumePainting();
Can colorizing of lines be speeded up in a RichtextBox?
For one of my projects, I have felt the need of a text editor with syntax highlighting. At first, I used a component inherited from RichTextBox, but while using it for a large amount of text I found out that RichTextBox highlights very slowly a large number of colored fragments (from 200 and more). When such highlighting has to be made in a dynamic way, it causes a serious problem.
http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting
I reckon RichTextBox is just a b*** at times...
Temporaily disabling the C# Rich Edit undo buffer while performing syntax highlighting
You will have to handle undo/redo yourself instead of relying on RTB, which means hooking into the keyboard events to listen for CTRL+Z, etc.
You can see how this author from codeproject did it for a similar RTB-overridden syntax highlighting editor: http://www.codeproject.com/KB/edit/SyntaxHighlighting.aspx
Related Topics
How to Use Openfiledialog to Select a Folder
How to Read a Pem Rsa Private Key from .Net
How to Know If a Datetime Is Between a Daterange in C#
Most Efficient Way to Remove Special Characters from String
Get Last 10 Lines of Very Large Text File > 10Gb
Getting a Url with an Url-Encoded Slash
Use Multiple Jwt Bearer Authentication
Getting the Http Referrer in ASP.NET
Converting a Base 64 String to an Image and Saving It
Regex for Accepting Only Persian Characters