Daniel Lip
Daniel Lip

Reputation: 11319

How can I color in a richTextBox only the result text and not all the text in red?

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    if (results.Count > 0)
    {
        richTextBox1.SelectionStart = results[(int)numericUpDown1.Value - 1];
        richTextBox1.ScrollToCaret();
        richTextBox1.SelectionColor = Color.Red;
    }
}

I want that when changing the numericUpDown it will color like highlight only the result :

results[(int)numericUpDown1.Value - 1]

Doing ForeColor is coloring all the text in the richTextBox.

I forgot to mention that before this I have a function that highlight specific text in the richTextBox :

void HighlightPhrase(RichTextBox box, string phrase, Color color)
        {
            int pos = box.SelectionStart;
            string s = box.Text;
            for (int ix = 0; ; )
            {
                int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
                if (jx < 0)
                {
                    break;
                }
                else
                {
                    box.SelectionStart = jx;
                    box.SelectionLength = phrase.Length;
                    box.SelectionColor = color;
                    ix = jx + 1;
                    results.Add(jx);
                }
            }
            box.SelectionStart = pos;
            box.SelectionLength = 0;
        }

And using it :

string word = textBox1.Text;
                    string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
                    foreach (string myword in test)
                    {
                        HighlightPhrase(richTextBox1, myword, Color.Yellow);
                        label16.Text = results.Count.ToString();
                        label16.Visible = true;
                        if (results.Count > 0)
                        {
                            numericUpDown1.Maximum = results.Count;
                            numericUpDown1.Enabled = true;
                            richTextBox1.SelectionStart = results[(int)numericUpDown1.Value - 1];
                            richTextBox1.ScrollToCaret();
                        }
                    }

Upvotes: 0

Views: 49

Answers (2)

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

Whenever you want to change the color / font / indentation / tabs etc of only part of the RichTextBox, first select the part that you want to change, then call one of the Selection... methods of the RichTextBox

RichTextBox richTextbox = ...
int startSelect = ...
int selectionLength = ...
richTextbox.Select(startSelect, selectionLength);

// color the selected part red.
richTextBox.SelectionColor = System.Drawing.Color.Red;

// if desired: change other properties of the selected part
System.Drawing.Font currentFont = richTextBox1.SelectionFont;
richTextBox1.SelectionFont = new Font(
     currentFont.FontFamily, 
     currentFont.Size, 
     FontStyle.Bold);

Check the Select... properties of class RichTextBox

Upvotes: 1

Lotan
Lotan

Reputation: 4283

Color color = Color.red;    
string result = "Result";
string nonColoredPart = "This is not colored";
string partialResultColored = "<color=#" + ColorUtility.ToHtmlStringRGBA(color) + ">" + result+ "</color>" + nonColoredPart;

And then assign the text to the place that accepts Rich Text.

Upvotes: 1

Related Questions