mixing font attributes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    mixing font attributes

    So - I have some text in an RTF control. I want the user to be able to highlight text and apply an attribute. The following code is placed into a button called BOLD and seems to work nicely:

    Code:
         If rtfText.SelectedText.Length > 0 Then
               rtfText.SelectionFont = New Font(rtfText.SelectionFont, FontStyle.Bold)
         End If
    The code above will turn the selected text bold. Great!
    But then the user selects the Same text and with the following code placed into a button called ITALIC it removes the bold attribute and replaces it with italics.

    Code:
          If rtfText.SelectedText.Length > 0 Then
              rtfText.SelectionFont = New Font(rtfText.SelectionFont, FontStyle.Italic)
           End If
    Since the users actions are separate - I can not know that they want BOLD and ITALIC, so how do I make sure I keep the existing attribute on the selected text (which, by the way could be part bold and part not-bold) and add italic?

    Somehow I need to add the italic attribute to the selected text, not simply replace all attributes with italic.
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Ok SOME progress, but not there yet....

    Here's new code:

    For Bold
    Code:
    If rtfText.SelectedText.Length > 0 Then
               rtfText.SelectionFont = New Font(rtfText.SelectionFont, FontStyle.Bold Or rtfText.SelectionFont.Style)
    End If
    For Italic
    Code:
    If rtfText.SelectedText.Length > 0 Then
               rtfText.SelectionFont = New Font(rtfText.SelectionFont, FontStyle.Italic Or rtfText.SelectionFont.Style)
    End If

    Notice that I added the OR, and apply the new font style to the current. This works well, but only when the second selection is of text with all the same attribute.
    If I select a section of text and make it bold, then select a portion of the bold text along with a portion of the normal text then the above italic code will make everything just italic - removing the bold

    Please don't tell me I have to go character by character - that would suck!
    Granted - somebody has to, but I would prefer it be the lower level routine...

    Comment

    Working...