Freitag, 19. Juni 2009

Adding Text with different color styles in RichTextBox


/// <summary>

/// Writing to a RichTextBox in different colors should be easy enough. The intiuitional way would be:

/// richTextBox1.Text += someText;

/// richTextBox1.Select(richTextBox1.Find(someText), someText.Length);

/// richTextBox1.SelectionColor = Color.Blue;

/// Which works but gets messed up when other text is added to the RichTextBox.

/// Adding SelectedText instead of just Text does the trick:

/// </summary>

foreach (string ws in WorksheetsRequired)

{

if (this.Worksheets.ContainsKey(ws))

{

string ok = "Worksheet " + ws + " found." + Environment.NewLine;

output.SelectionColor = Color.Green;

output.SelectedText += ok;

}

else

{

string error = "Worksheet " + ws + " could not be found." + Environment.NewLine;

output.SelectionColor = Color.Red;

output.SelectedText += error;

}


// scroll to bottom of rtb
output.SelectionStart = this.richTextBox1.Text.Length;

output.ScrollToCaret();

}

Keine Kommentare:

Kommentar veröffentlichen