Live Chat Icon For mobile
Live Chat Icon

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

Platform: WinForms| Category: RichTextBox

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).

private void AddFontStyle(FontStyle style)
{
  int start = richTextBox1.SelectionStart;
  int len = richTextBox1.SelectionLength;
  System.Drawing.Font currentFont;
  FontStyle fs;
  for(int i = 0; i < len; ++i)
  {
    richTextBox1.Select(start + i, 1);
    currentFont = richTextBox1.SelectionFont;
    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;
  System.Drawing.Font currentFont;
  FontStyle fs;
  for(int i = 0; i < len; ++i)
  {
    richTextBox1.Select(start + i, 1);
    currentFont = richTextBox1.SelectionFont;
    fs = currentFont.Style;
    //remove style
    fs = fs & ~style;
    richTextBox1.SelectionFont = new Font(
      currentFont.FontFamily, 
      currentFont.Size, 
      fs
      );
  }
}

private void button1_Click(object sender, System.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 change_font(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 < lengt; i++) {
                        richTextBox2.Select(i,1);
                        Font cfont = richTextBox2.SelectionFont;
                        FontStyle fs = cfont.Style;
                        if (add) {
                              fs = fs | style;
                        }
                        else {
                              fs = 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);
                  this.richTextBox1.Focus();
                  isChanged = true;
            }

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.