title: Adjusting font style for a RichTextBox in vb.net
body: <p>I'm having trouble adjusting the font style in a RichTextBox and I've seen a few different approaches that talk about single attributes (like toggling bold on and off)... but I'm trying to make it so that my font class can adjust <em>any</em> attribute (bold, italic, underline).</p>
  
  <p>I realize that Font.Style is a set of Boolean flags (a bitfield?)... but I'm not sure how to handle the attributes all at once.</p>
  
  <p>Here is the troublesome code:</p>
  
  <pre><code>Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _
                             Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing)
      Dim newFontStyle As System.Drawing.FontStyle
  
  
      If Plain Then
          newFontStyle = Drawing.FontStyle.Regular
          GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
          Exit Sub
      End If
  
      If Bold IsNot Nothing Then
          If Bold Then
              newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold
          Else
              newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold
          End If
      End If
  
  
      If Italics IsNot Nothing Then
          If Italics Then
              newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic
          Else
              newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic
          End If
      End If
  
      If Underlined IsNot Nothing Then
          If Underlined Then
              newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline
          Else
              newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline
          End If
      End If
  
      GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle)
  
  End Sub
  </code></pre>
  
  <p>And here is the eventual trouble with this code:</p>
  
  <ul>
  <li>I toggle bold (to true) - the text goes bold.</li>
  <li>I toggle underlined - the text is now bold and underlined.</li>
  <li>I toggle italics - the text is now bold, underlined, and in italics.</li>
  <li>I toggle bold again (to false) - the text is now strikethrough. </li>
  </ul>
  
  <p>What is happening to the font? The text should be underlined and in italics not strikethrough...</p>
  
  <p>Is this a logic error or a simple misunderstanding on my part?</p>
  
  <p>Well, thank you for your time, I'll keep tinkering around with it until it is working or I get an answer that works,</p>