1

Configuration:

  • Windows 7
  • .NET 3.5
  • Visual Studio 2008

Summary: sending words to a rtb via a for-loop; format them based on their content ("OK" shown in green, "failed" shown in red).

Code:

for (int i = 0; i < inputValues.Length; i++)
{
    //checking if the value is OK or not
    string answer = functionReturningString(inputValues[i], referenceValue); 
    textBox4.Text += answer; //and sending the result string to text box
}

Now I'm simply trying to select the string that was added last and format it based on its content.

textBox4.SelectAll();
textBox4.Select(textBox4.SelectedText.Length - answer.Length, answer.Length);

if (answer == "OK")
{
    textBox4.SelectionColor = Color.Green;
} else {
    textBox4.SelectionColor = Color.Red;
}

textBox4.Refresh();//I want to see every value as soon as added
textBox4.Text += "\r\n"; //adding space between words

As for the result, it will eventually use the "SelectionColor" on all words in the rtb.

Q: How can I make sure the previously formatted words don't change their color again?

Update: The suggested solution didn't work either. Words will be displayed in their correct color (at first). Then the next word is added and the color of the whole box changes.

Avigrail
  • 321
  • 2
  • 4
  • 14

1 Answers1

2

The sequence should be something like this (assuming you start with empty rich text box):

richTextBox.SelectionColor = some_Color;
richTextBox.AppendText(some_Text);

Here is an example simulating the case you have described (if I understood it correctly):

using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Tests
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            var richTextBox = new RichTextBox { Dock = DockStyle.Fill, Parent = form };
            var button = new Button { Dock = DockStyle.Bottom, Parent = form, Text = "Test" };
            button.Click += (sender, e) =>
            {
                Color TextColor = Color.Black, OKColor = Color.Green, FailedColor = Color.Red;
                var questions = Enumerable.Range(1, 20).Select(n => "Question #" + n).ToArray();
                var random = new Random();
                richTextBox.Clear();
                for (int i = 0; i < questions.Length; i++)
                {
                    richTextBox.SelectionColor = TextColor;
                    richTextBox.AppendText(questions[i] + ":");
                    bool ok = (random.Next() & 1) != 0;
                    richTextBox.SelectionColor = ok ? OKColor : FailedColor;
                    richTextBox.AppendText(ok ? "OK" : "Failed");
                    richTextBox.SelectionColor = TextColor;
                    richTextBox.AppendText("\r\n");
                }
            };
            Application.Run(form);
        }
    }
}

which produces the following

enter image description here

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Wow, that one works just fine! I didn't know you can do things like (ok ? "OK" : "Failed") Thanks a lot :) – Avigrail Oct 15 '15 at 08:51