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:
- 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?
- 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()