Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Changing font style of WinForm RichTextBox without selecting the text
    primarykey
    data
    text
    <p>I am using a <code>RichTextBox</code> in my code where I show syntax-highlighted code. Now, on every keystroke, I have to re-parse all the tokens and re-color them all over again. But the only way to color individual words in a <code>WinForm richtextbox</code> is to select those words one by one and color them using SelectionFont. </p> <p>But if the user is typing really fast, there is a very noticeable flickering caused by my selecting individual words (selected words have that Windows blue-background thing and that is causing the flickering). Is there any way around that where I can color individual words without selecting them (and hence causing that blue highlight around the selected text). I tried using <code>SuspendLayout()</code> to disable rendering during my coloring but that didn't help. Thanks in advance!</p> <p>Here is my code: </p> <p>Code: </p> <pre><code>private void editBox_TextChanged(object sender, EventArgs e) { syntaxHighlightFromRegex(); } private void syntaxHighlightFromRegex() { this.editBox.SuspendLayout(); string REG_EX_KEYWORDS = @"\bSELECT\b|\bFROM\b|\bWHERE\b|\bCONTAINS\b|\bIN\b|\bIS\b|\bLIKE\b|\bNONE\b|\bNOT\b|\bNULL\b|\bOR\b"; matchRExpression(this.editBox, REG_EX_KEYWORDS, KeywordsSyntaxHighlightFont, KeywordSyntaxHighlightFontColor); } private void matchRExpression(RichTextBox textBox, string regexpression, Font font, Color color) { System.Text.RegularExpressions.MatchCollection matches = Regex.Matches(this.editBox.Text, regexpression, RegexOptions.IgnoreCase); foreach (Match match in matches) { textBox.Select(match.Index, match.Length); textBox.SelectionColor = color; textBox.SelectionFont = font; } } </code></pre> <p>Inside the MyRichTextBox (dervied from RichTextBox): </p> <pre><code>public void BeginUpdate() { SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero); } public void EndUpdate() { SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); } [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); private const int WM_SETREDRAW = 0x0b; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload