RichTextBox provides the method AppendText() inherited from TextBoxBase to programmatically add text to the editor control, but there seems to be no native method to append formatted text.![]()
A small extension class adds this functionality:
public static class RichTextBoxExtensions
{
const string NewLine = "\r\n";
public static void AppendLine(this RichTextBox ed)
{
ed.AppendText(NewLine);
}
public static void AppendLine(this RichTextBox ed, string s)
{
ed.AppendText(s + NewLine);
}
public static void AppendBoldLine(this RichTextBox ed, string s)
{
int ss = ed.SelectionStart;
ed.AppendText(s);
int sl = ed.SelectionStart - ss + 1;
Font bold = new Font(ed.Font, FontStyle.Bold);
ed.Select(ss, sl);
ed.SelectionFont = bold;
ed.AppendText(NewLine);
}
}
Advertisement

Thank you very much!. Very very useful!!!