I have the following code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PentruTestari
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox2.Text = "int int";
            richTextBox2.SelectionStart = 0;
            richTextBox2.SelectionLength = 7;
            richTextBox2.SelectionColor = Color.Red;
            richTextBox2.SelectionLength = 0;
            richTextBox2.SelectionStart = 4;
            richTextBox2.SelectionColor = Color.Black;
        }
        private void richTextBox2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(richTextBox2.SelectionColor.ToString());
        }
    }
}

Every time I click right before the second "int" , the SelectionColor is black. But when I click at the middle of the first "int" and then click back at the beginning of the first "int" the SelectionColor is red and it should remain black.
I have this problem in a project I am working at and this is the reproduction of the problem.

I will give any extra details.

Dani AI

Generated

This behavior is expected — not a mysterious bug — and comes from how the WinForms RichTextBox reports and stores formatting. ’s final note (selecting the trailing space) is exactly the right practical fix; the thread replies from and point toward the root cause (overlapping/ambiguous formatting runs and caret placement) rather than a framework error.

SelectionColor reflects the formatting of the current selection or the insertion point. If a selection contains more than one color, SelectionColor returns an “empty” result; if no text is selected, SelectionColor represents the color at the insertion point and is used for text typed after the caret. Under the hood the control queries character-format info (CHARFORMAT / EM_SETCHARFORMAT), so the property follows the RTF/char-format run model rather than “word-level” rules. (learn.microsoft.com)

Why selecting the space helps: when you explicitly color the space you create an unambiguous formatting run that covers the inter-word gap, so clicks that end up there report the color you expect. If the space is left with the other word’s color, clicking near the boundary can put the caret in the neighboring run and return that run’s color. For deterministic per-character reads, select a single character and read SelectionColor (a multi-character selection can return Color.Empty). (stackoverflow.com)

Best-practice checklist for predictable results

  • Always Select the exact range you want to color (include the space if you want the gap to carry that color).
  • To set the typing color, set SelectionStart, SelectionLength = 0 and then SelectionColor (it applies to the insertion point).
  • When scanning text programmatically, inspect one character at a time if you need precise colors.
  • Preserve and restore the user selection or use a window-update lock to avoid flicker while you change formats. (codeproject.com)

These rules explain the observed click-dependent change and give a robust way to avoid it in syntax-highlighting or formatting code.

Recommended Answers

All 4 Replies

Is it due to the overlapping selection lengths? You have 0 to 7 and 0 to 4 which obviously both include 0-4.

Well I firstly select from 0 to 7 (lines 22 and 23 ) , then I deselect (line 25) and then I set the selection start at index 4 (line 26) , so there is one selection, I think.

When I try your code it doesn't duplicate the problem you mention. But when I switch the values in lines 25 & 26 I get one "int" in black and one in red. Whenever I click on any of the first 4 characters(including the space) it reports black and on any of the last 3 it reports red. I couldn't get the change that you reported.

I managed to resolve the problem by selecting the space after the word. Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.