Search code examples
c#winformscolorsrichtextboxselectedtext

Color change of selected text not working


Hey Guys im currently stuck in writing a Debug function where my Exception should gets printed in Red to an RichTextBox. I tried different solutions i've found here but they are not working, so i think i'm doing something wrong.

Here is my Function where the Text gets printed in Red:

public void DebugHighlighter(string s)
    {
        /*
        richTextBoxOutput.SelectionColor = Color.Red;
        richTextBoxOutput.SelectedText = s;
        
        richTextBoxOutput.SelectionColor = Color.Red;
        richTextBoxOutput.Text += s + "\n";
        richTextBoxOutput.Find(s);
        */

        richTextBoxOutput.SelectionColor = Color.Red;
        richTextBoxOutput.AppendText(s);
        richTextBoxOutput.AppendText("\n");
        int index = richTextBoxOutput.Text.IndexOf(s);
        int lenght = s.Length;
        richTextBoxOutput.Select(index, lenght);
    }

Here is the function that gives the string to the DebugHighlighter:

try
            {
                if (!reversed)
                {
                    string outputSplitter = Regex.Replace(output2[19], @"[a-zA-z]", " ");
                    outputBandwith = outputSplitter.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    bandwithResult = outputBandwith[3];
                }
                else if (reversed)
                {
                    string outputSplitter = Regex.Replace(output2[20], @"[a-zA-z]", " ");
                    outputBandwith = outputSplitter.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    bandwithResult = outputBandwith[3];
                }
            }
            catch(IndexOutOfRangeException ex)
            {
                OutputConsoleForm._OutputConsoleForm.DebugHighlighter("Index out of Range");
            }

I will also attach an Screenshot of the Output(I put a box on the Output which should be printed in red).

I hope someone can tell me the little or bigger thing which im doing wrong here. I already looked on reddit/stackoverflow/msdn but no variant works out for me.

Image of the Output with the Bug


Solution

  • To change the color of the text you want, first add it, then select it, and finally set the SelectionColor property.

    Output:

    Output

        public void DebugHighlighter(string firstParagraph, string s, string lastParagraph, RichTextBox RTB)
        {
            RTB.AppendText(firstParagraph);
            RTB.AppendText(s + "\n");
            RTB.Select(firstParagraph.Length, s.Length);
            RTB.SelectionColor = Color.Red;
            RTB.AppendText(lastParagraph);
    
        }
    
        private void CheckButton_Click(object sender, EventArgs e)
        {
            //This part is only for creating exceptions, and I only used it as an example.
            int Max = int.Parse(TextBox.Text);
            int[] Array = new int[7];
            Random RandomNumber = new Random();
            try
            {
                for (int i = 0; i < Max; i++)
                {
                    Array[i] = RandomNumber.Next(0, 100);
                }
                MessageBox.Show(Array[6].ToString());
            }
            catch (IndexOutOfRangeException)
            {
                RichTextBox.Text = "";
                string FirstParagraph = "Your paragraph...\n";
                string LastParagraph = "Your paragraph...";
                DebugHighlighter(FirstParagraph, "IndexOutOfRangeException", LastParagraph, RichTextBox);
            }
        }
    
        private void ClearRichTextBox_Click(object sender, EventArgs e)
        {
            RichTextBox.Text = "";
        }
    

    Tested in:

    Visual Studio 2017, .NET Framework 4.5.2, Windows Forms

    Best regards,

    Reza Jaferi