Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

first the information you may require, i'm using .NET 3.5.

My Problem is, that my RichTextBox does not want to color textparts. I already asked google and tried 10 different way's. I also searched codeprojekt. Here is the Codepart:

C#
this.BeginInvoke(new MethodInvoker(delegate()
            {
                output.SelectionStart = output.Text.Length;
                //output.SelectionFont = font;
                output.SelectionColor = Color.Red;
                output.AppendText(text);
                Application.DoEvents();
            }));


But the Text is still not Red -.-
Posted

you need to set the SelectionLength as well.

For that matter, I don't think I understand what you're trying to do.

Are you trying to append text to the output and then highlight it? If so, you've got your code all mixed up. First, you append the text. Then, you set the SelectionStart to where the text begins. Then, you set the SelectionLength to the length of the added text. Then you change the SelectionColor.
 
Share this answer
 
v2
Comments
diialer 21-Oct-10 17:39pm    
You mean like this?

this.BeginInvoke(new MethodInvoker(delegate()
{
int selectionStart = output.Text.Length;
output.AppendText(text);
output.SelectionStart = selectionStart;
output.SelectionLength = text.Length; // Even without this line it does not work...
output.SelectionColor = Color.Red;
}));


First i get the startposition, cuz the RichTextBox is not empty. Then i append the text and start the selection at for example 20 with the length 10 and color it in red.

Still does not work... -.-
What you need to do is note the position of the start of the selection, append the text, select the appended text and then change the color of the selection.
 
Share this answer
 
Ok, so, first, don't post an answer that isn't actually an answer.

As an FYI, I created a form with just a RichTextBox, a Button, and a BackgroundWorker. Here's the code for it:

C#
private void button1_Click_1(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync(richTextBox1);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    RichTextBox caller = (RichTextBox)e.Argument;
    string text = "Test";
    if (caller.InvokeRequired)
    {
        caller.BeginInvoke(new MethodInvoker(delegate()
        {
            int selectionStart = caller.Text.Length;
            caller.AppendText(text);
            caller.SelectionStart = selectionStart;
            caller.SelectionLength = text.Length;
            caller.SelectionColor = Color.Red;
        }));
    }
}


It adds the string "Test" to the RichTextBox and makes the color red. So if it's not working for you, you're doing something wrong.
 
Share this answer
 
Comments
diialer 21-Oct-10 17:56pm    
Excuse me for the "fake answer" but i'd not see the [Add Comment]-Button.
I tried without invoke the RichTextBox delegate in a new Form without try'ing to manipulate a thread from another class and it works fine. But if i want to write something into the RichTextBox from another class it does not work to color it??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900