John Michael Wilkinson
John Michael Wilkinson

Reputation: 145

How Do I Retain Font Style and Size When Selecting Bold, Italic or Underline?

I looked at solutions to other similar questions, but they don't appear to answer my question? In the code below, I given examples of the code behind my Font Style Picker Combo and my Font Size Combo and my Bold Toolbar button. I have not included Italic and Underline code because perhaps Bold is sufficient? The code works fine, but see the issues I'm having below! If anyone can give an example of how to adapt this code (or other code) to make it work as desired, I would greatly appreciate it!

Issues:

  1. If I select some text in my Rich Text Box and hit Bold, Italic or Underline buttons on the toolbar of my app, this works fine. However, these styles are mutually exclusive so applying Bold removes Italic or underline, etc. Can't get it to work to apply Bold + Italic, Bold + Underline, etc?
  2. If I apply a Font Style (e.g. Arial) and Size (e.g. 20), then hit Bold, Italic or underline, the font is restored to default style and size (i.e. Microsoft Sans Serif, 8,25) and then bold is applied? I want to be able to apply several styles to the same selected text in RTB without losing the style or size I already applied? (I checked this out in WPS Writer and this is possible!). Also, I want to be able to apply a single style independently without being obliged to apply others?

What I have tried:

I copied the code from the Font Style Picker Combo and Font Size Combo and created a Sub Routine for each. Then, I called that code in the code behind the Bold, Italic and Underline buttons as can be seen below. This works, but throws a null exception when I don't select a Font Style and Font Size before applying Bold, Italic or Underline. I understand this behaviour, i.e. I'm calling the Font Style and Font Size subs which result in null if I don't select anything. But if I use this solution to the issue then I need to do some error handling and the user is obliged to select a font style and size to be able to use Bold, Italic or underline? I want to be able to apply a single style/size or several styles/size independently. (FStyle and FSize are the calls for the sub routines using the code from the Font Style and Size Picker Combos!).

Code:

'Font Picker Combo Box:
Private Sub tbSelectFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbSelectFont.SelectedIndexChanged

        Dim NewFont As New Font(tbSelectFont.SelectedItem.ToString(),
                                Description.SelectionFont.Size,
                                Description.SelectionFont.Style)
        Description.SelectionFont = NewFont

'Font Size Picker Combo Box:
Private Sub tbSelectSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbSelectSize.SelectedIndexChanged
        Dim NewSize As Single = tbSelectSize.SelectedItem
        Dim NewFont As New Font(Description.SelectionFont.Name, NewSize, Description.SelectionFont.Style)
        Description.SelectionFont = NewFont
    End Sub

'Bold Toolbar Button:
Private Sub tbBold_Click(sender As Object, e As EventArgs) Handles tbBold.Click
        Dim bfont As New Font(Description.Font, FontStyle.Bold)
        Dim rfont As New Font(Description.Font, FontStyle.Regular)
        If Description.SelectedText.Length = 0 Then Exit Sub
        If Description.SelectionFont.Bold Then
            Description.SelectionFont = rfont
        Else
            Description.SelectionFont = bfont
        End If

        'Restore Font Style And Size:
        FStyle()
        FSize()

Upvotes: 0

Views: 929

Answers (2)

John Michael Wilkinson
John Michael Wilkinson

Reputation: 145

First, I would like to thank both Jimi and jmcilhinney for pointing me in the right direction! Thanks guys for your help! Then, I would like to thank DanieldotNET for his post on Slideshare.net which I am posting as a solution which works for me and is according to Jimi and jmcilhinney's suggestions (I found his post while I was studying what you guys posted here! I will have a go sometime later at building the solution you mention Jimi, Properly draw text using GraphicsPath, as it looks great and there's a lot I can learn there!). I built the demo according to DanieldotNET's tutorial and it works! But, I was thinking I would have to adapt the code to include the Font Type and Size to retain both when applying Bold, Italic or Underline. However, Daniel's code works for a single style or any combination of Bold, Italic or Underline (Strikeout also works if you adapt the code). It also retains the Font Type and Size without any further adaptation! However, I noticed that Daniel's code has a Copyright on it which I have included below! So, let's respect his code!

PROJECT: How To: Apply Bold, Italic and Underline Formatting to Selected Text in a .NET RichTextBox (Part 2) © DanieldotNET DEC2013

SOURCE CODE: https://www.slideshare.net/DanielDotNet/191474746-bolditalicunderlineinnetrich-textboxpart2visualbasic-37120968

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click

Dim style As FontStyle

' Test if there is Bold FontStyling in
' the Selection's FontStyle
If RichTextBox1.SelectionFont.Bold Then

' Since Bold Style Exists, undo the Bold Style
style = RichTextBox1.SelectionFont.Style _
And Not FontStyle.Bold

RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, style)

Else

' Since there is no Bold Style,
' apply the Bold Style
style = RichTextBox1.SelectionFont.Style _
Or FontStyle.Bold

RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, style)

End If

' Set input focus on the RichTextBox
RichTextBox1.Focus()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click

Dim style As FontStyle

' Test if there is Italic FontStyling in
' the Selection's FontStyle
If RichTextBox1.SelectionFont.Italic Then

' Since Italic Style Exists, undo the Italic Style
style = RichTextBox1.SelectionFont.Style _
And Not FontStyle.Italic

RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, style)

Else

' Since there is no Italic Style,
' apply the Italic Style
style = RichTextBox1.SelectionFont.Style _
Or FontStyle.Italic

RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, style)

End If

' Set input focus on the RichTextBox
RichTextBox1.Focus()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button3.Click

Dim style As FontStyle

' Test if there is Underline FontStyling in
' the Selection's FontStyle
If RichTextBox1.SelectionFont.Underline Then

' Since Underline Style Exists, undo the Underline Style
style = RichTextBox1.SelectionFont.Style _
And Not FontStyle.Underline

RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, style)

Else

' Since there is no Underline Style,
' apply the Underline Style
style = RichTextBox1.SelectionFont.Style _
Or FontStyle.Underline

RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, style)

End If

' Set input focus on the RichTextBox
RichTextBox1.Focus()

End Sub

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54427

'Apply the Bold style.
myFont = New Font(myFont, myFont.Style Or FontStyle.Bold)

'Remove the Bold style.
myFont = New Font(myFont, myFont.Style And Not FontStyle.Bold)

'Toggle the Bold style.
myFont = New Font(myFont, myFont.Style Xor FontStyle.Bold)

All those operators are bitwise rather than Boolean. A FontStyle is a composite value where each bit represents an individual style. The Or operator will set a bit in the result if it is set in either of the operands, so myFont.Style Or FontStyle.Bold will ensure that all styles that were originally set will remain set and no new styles other than Bold will be set. The And operator will set a bit in the result if it is set in both operands and Not FontSyle.Bold has every bit but the Bold bit set, so every bit in the original style that was set, other than the Bold bit, will be set in the result. The Xor operator will set a bit in the result if it is set in one and only one of the operands.

Upvotes: 2

Related Questions