Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I change the FontStyle of a selection in a RichTextBox without losing the styles that are present?

If you visit the selection a character at the time, you can get the current FontStyle and modify it directly to add or remove a style like Bold or Italic. Doing it a character at a time will avoid losing the other styles that are set. The problem with doing it a whole selection at a time is that the FontStyle of the entire selection is the common styles set among all characters in the selection. So, if one char is bold and one is not, then bold is not set when you retrieve the fontstyle for the entire selection. Doing things a character at the time, avoids this problem and allows things to work OK. You can download a working sample (CS, VB).

using System.Drawing;

private void AddFontStyle( FontStyle style )
{
  int start = richTextBox1.SelectionStart;
  int len = richTextBox1.SelectionLength;
  for ( int i = 0; i < len; ++i )
  {
    richTextBox1.Select(start + i, 1);
    Font currentFont = richTextBox1.SelectionFont;
    FontStyle fs = currentFont.Style;

    //add style
    fs = fs | style;
    richTextBox1.SelectionFont =
      new Font( currentFont.FontFamily, currentFont.Size, fs );
  }
}

private void RemoveFontStyle( FontStyle style )
{
  int start = richTextBox1.SelectionStart;
  int len = richTextBox1.SelectionLength;
  for ( int i = 0; i < len; ++i )
  {
    richTextBox1.Select(start + i, 1);
    Font currentFont = richTextBox1.SelectionFont;
    FontStyle fs = currentFont.Style;

    //remove style
    fs = fs & ~style;
    richTextBox1.SelectionFont =
      new Font( currentFont.FontFamily, currentFont.Size, fs );
  }
}

Sample usage:

private void button1_Click( object sender, EventArgs e )
{
  AddFontStyle(FontStyle.Bold);
}

Here is a suggestion from Nicholas Clark on how to avoid seeing the character by character changes appear as you apply the styles one character at a time in the above code. Create a hidden RTB and store the selected text in it. Then apply the attributes and copy it back into the original richtextbox, so that you don't see the selection changing.

private void ChangeFont( FontStyle style, bool add )
{
  richTextBox2.Rtf = richTextBox1.SelectedRtf;
  int lengt = richTextBox2.Text.Length;
  int length = richTextBox1.SelectionLength;
  int start = richTextBox1.SelectionStart;
  for ( int i = 0; i < length; ++i )
  {
    richTextBox2.Select( i, 1 );
    Font cfont = richTextBox2.SelectionFont;
    FontStyle fs = cfont.Style;
    fs = add ? fs | style : fs & ~style;

    richTextBox2.SelectionFont =
      new Font( cfont.FontFamily, cfont.Size, fs );
  }

  richTextBox2.Select( 0, richTextBox2.Text.Length );
  richTextBox1.SelectedRtf = richTextBox2.SelectedRtf;
  richTextBox1.Select( start, length );
  richTextBox1.Focus();
  isChanged = true;
}

George Shepherd, Syncfusion, and Nicholas Clark



Page view counter