Windows Develop Bookmark and Share   
 index > Windows Forms General > Richtextbox->selectioncolor, how to use?
 

Richtextbox->selectioncolor, how to use?

Hi,
I have a windows form application written in VC++ .net.

I have a richtextbox "Log" used as a logging events and errors.

I want to be able to output most messages in black but error messages in red.

I have the following code written.

Code:
Log->Text=Log->Text+"ERROR, you must specify a existing directory for source. \n";
int index1 = Log->Text->LastIndexOf("ERROR");
int index2 = Log->Text->LastIndexOf("\n");
Log->Select(index1, index2);
Log->SelectionColor=System:Big Smilerawing::Color::Red;


So I selected the error message line use select on index1 and index2, then changed the color using selection color. It works, except when I append more text to the Log such as

Code:
Log->Text=Log->Text+"more log \n";


This will change the color back to black. I don't know why it is doing this, is there a way to keep the selectioncolor?

Thanks
foxhunt99  Thursday, March 06, 2008 8:12 PM

Hi foxhunt99,

As you have seen, once a new line is inserted into the RichTextBox. The RichTextBox.SelectionStart and SelectionLength properties are set to Zero. You need to manually reselect the “Error Text�and reset their color. This is not a convenient way, but it works. If anyone else has a better approach, please share it with us.

In the following code sample, RichTextBox1_TextChanged event handler is used to monitor the change of the text and reselect the “Error Text�

Code Snippet

partial class Form1 : Form

private void Form1_Load(object sender, EventArgs e)

{

richTextBox1.Text += "#1 adfadf" + "\r\n";

richTextBox1.Text += "#2 index1, index2" + "\r\n";

richTextBox1.Text += "#3 adsfasdf" + "\r\n";

richTextBox1.Text += "#4 adsfasdf" + "\r\n";

SelectErrorText();

}

private void SelectErrorText()

{

int oldSelectionStartIndex = richTextBox1.SelectionStart;

int oldSelectionLength = richTextBox1.SelectionLength;

int secondLine = richTextBox1.GetFirstCharIndexFromLine(1);

int thirdLine = richTextBox1.GetFirstCharIndexFromLine(2);

richTextBox1.SelectionStart = secondLine;

richTextBox1.SelectionLength = thirdLine - secondLine;

richTextBox1.SelectionColor = Color.Red;

richTextBox1.Select(oldSelectionStartIndex, oldSelectionLength);

}

private void insertNewLineButton_Click(object sender, EventArgs e)

{

richTextBox1.Text += "A New Line" + "\r\n";

}

private void richTextBox1_TextChanged(object sender, EventArgs e)

{

if (richTextBox1.TextLength >= 30)

{

SelectErrorText();

}

}

Best regards,

Jacob

Jacob Sui - MSFT  Monday, March 10, 2008 5:19 AM

Hi foxhunt99,

As you have seen, once a new line is inserted into the RichTextBox. The RichTextBox.SelectionStart and SelectionLength properties are set to Zero. You need to manually reselect the “Error Text�and reset their color. This is not a convenient way, but it works. If anyone else has a better approach, please share it with us.

In the following code sample, RichTextBox1_TextChanged event handler is used to monitor the change of the text and reselect the “Error Text�

Code Snippet

partial class Form1 : Form

private void Form1_Load(object sender, EventArgs e)

{

richTextBox1.Text += "#1 adfadf" + "\r\n";

richTextBox1.Text += "#2 index1, index2" + "\r\n";

richTextBox1.Text += "#3 adsfasdf" + "\r\n";

richTextBox1.Text += "#4 adsfasdf" + "\r\n";

SelectErrorText();

}

private void SelectErrorText()

{

int oldSelectionStartIndex = richTextBox1.SelectionStart;

int oldSelectionLength = richTextBox1.SelectionLength;

int secondLine = richTextBox1.GetFirstCharIndexFromLine(1);

int thirdLine = richTextBox1.GetFirstCharIndexFromLine(2);

richTextBox1.SelectionStart = secondLine;

richTextBox1.SelectionLength = thirdLine - secondLine;

richTextBox1.SelectionColor = Color.Red;

richTextBox1.Select(oldSelectionStartIndex, oldSelectionLength);

}

private void insertNewLineButton_Click(object sender, EventArgs e)

{

richTextBox1.Text += "A New Line" + "\r\n";

}

private void richTextBox1_TextChanged(object sender, EventArgs e)

{

if (richTextBox1.TextLength >= 30)

{

SelectErrorText();

}

}

Best regards,

Jacob

Jacob Sui - MSFT  Monday, March 10, 2008 5:19 AM

You can use google to search for other answers

Custom Search

More Threads

• Handling title-bar buttons...
• Postback not happening on selectign a treeview node
• KeyUp problem on drawing
• how to use canel button in win application
• How to unload Forms correctly?
• Bitmap.GetHBitmap() returns invalid handle
• office 2003 icons...
• problem with database path ,only when browsing another files thru opendialog box in c#
• simple c# question
• Problem with label text