title: How do I maintain RichText formatting (bold/italic/etc) when changing any one element?
body: <p>I have a rich text box that may contain a string that has elements of bold, italics, or even different fonts and sizes. If I select the entire string, including all of the differences, how can I "Bold" that string without converting the entire string to the generic font with just a "bold" attribute?</p>
  
  <p>For example: I want to turn "This <em>is some</em> <code>text</code>" into  "<strong>This <em>is some</em> <code>text</code></strong>"</p>
  
  <p>Note that "is some" remained italicized and "text" remained a different font.</p>
  
  <p>What I currently have is quite simplistic:</p>
  
  <pre><code>private void tsBold_Click(object sender, EventArgs e)
  {
      if (rtb.SelectionFont == null) return;
  
      Font f;
  
      if (tsBold.Checked)
          f = new Font(rtb.SelectionFont, FontStyle.Bold);
      else
          f = new Font(rtb.SelectionFont, FontStyle.Regular);
  
      rtb.SelectionFont = f;
  
      rtb.Focus();
  }
  </code></pre>
  
  <p>Of course, this is going to apply the exact same font to the entire selection. Is there any way to just append "bold" to the existing font(s)?</p>
  
  <p><strong>ANSWER</strong>
  While the "official" answer below is just the tip of the iceberg, it was the push I needed in the right direction. Thank you for the tip.</p>
  
  <p>Here's my official fix:</p>
  
  <p>I added this to my RichTextBox object:</p>
  
  <pre><code>    /// &lt;summary&gt;
      ///     Change the richtextbox style for the current selection
      /// &lt;/summary&gt;
      public void ChangeFontStyle(FontStyle style, bool add)
      {
          //This method should handle cases that occur when multiple fonts/styles are selected
          // Parameters:-
          //  style - eg FontStyle.Bold
          //  add - IF true then add else remove
  
          // throw error if style isn't: bold, italic, strikeout or underline
          if (style != FontStyle.Bold
              &amp;&amp; style != FontStyle.Italic
              &amp;&amp; style != FontStyle.Strikeout
              &amp;&amp; style != FontStyle.Underline)
              throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyle");
  
          int rtb1start = this.SelectionStart;
          int len = this.SelectionLength;
          int rtbTempStart = 0;
  
          //if len &lt;= 1 and there is a selection font then just handle and return
          if (len &lt;= 1 &amp;&amp; this.SelectionFont != null)
          {
              //add or remove style 
              if (add)
                  this.SelectionFont = new Font(this.SelectionFont, this.SelectionFont.Style | style);
              else
                  this.SelectionFont = new Font(this.SelectionFont, this.SelectionFont.Style &amp; ~style);
  
              return;
          }
  
          using (EnhancedRichTextBox rtbTemp = new EnhancedRichTextBox())
          {
              // Step through the selected text one char at a time    
              rtbTemp.Rtf = this.SelectedRtf;
              for (int i = 0; i &lt; len; ++i)
              {
                  rtbTemp.Select(rtbTempStart + i, 1);
  
                  //add or remove style 
                  if (add)
                      rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style | style);
                  else
                      rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style &amp; ~style);
              }
  
              // Replace &amp; reselect
              rtbTemp.Select(rtbTempStart, len);
              this.SelectedRtf = rtbTemp.SelectedRtf;
              this.Select(rtb1start, len);
          }
          return;
      }
  </code></pre>
  
  <p>I then changed the click methods to use the following pattern:</p>
  
  <pre><code>    private void tsBold_Click(object sender, EventArgs e)
      {
          enhancedRichTextBox1.ChangeFontStyle(FontStyle.Bold, tsBold.Checked);
  
          enhancedRichTextBox1.Focus();
      }
  </code></pre>