2

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

Paddy
  • 31
  • 4
  • 1
    You need to __first__ select text and only __then__ change its properties! – TaW Aug 07 '22 at 20:47
  • @TaW I've put the Selection Color now to the end of the function but selected text is still white. – Paddy Aug 07 '22 at 21:29
  • Use the debugger to check the SelectionStart and SelectionLength values when doing the color change. – TaW Aug 08 '22 at 02:01
  • I answered your question by displaying a `GIF` result. If the response is helpful, please accept it and vote it up. – R_J Aug 08 '22 at 03:52

1 Answers1

1

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

R_J
  • 52
  • 12
  • If the response is helpful, please accept it and vote it up. – R_J Aug 08 '22 at 03:49
  • 1
    Hey thank you for that helpfull and detailed answer. I tried to figure out your solutions but the text won't get red. But i found that the text is not Printed at the time the propertie Color.Red gets applied. I called the function later in the program and the Error is Red ! Thanks alot Reza ! – Paddy Aug 08 '22 at 06:22