Question RichTextBox: changing font size without changing families and styles

vblm

New member
Joined
Jan 19, 2010
Messages
4
Programming Experience
10+
Hi everybody,

I'm searching for a way to change the font size of selected text in a RichTextBox (rtf) having different font families (e.g. Arial and MS SansSerif) and font styles (underline, bold...) using the FontDialog, but without changing the families and styles.

The following code resets all the font attributes, which is not what I want:

Dim fdNF As New FontDialog
If fdNF.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBoxNotesDossier.SelectionFont = fdNF.Font
End If

Any idea ? Thx !
 
Just get the fontFamily and style before you apply the new font e.g.

VB.NET:
        Dim originalFontFamily As FontFamily = RichTextBoxNotesDossier.SelectionFont.FontFamily
        Dim originalFontStyle As FontStyle = RichTextBoxNotesDossier.SelectionFont.Style
        Dim fdNF As New FontDialog
        If fdNF.ShowDialog = Windows.Forms.DialogResult.OK Then
            RichTextBoxNotesDossier.SelectionFont = New Font(originalFontFamily, fdNF.Font.Size, originalFontStyle)
        End If
 
Just get the fontFamily and style before you apply the new font e.g.
That won't work, since SelectionFont in this case returns Nothing.
a way to change the font size of selected text in a RichTextBox (rtf) having different font families (e.g. Arial and MS SansSerif) and font styles
The common solution is to loop over the selection indexes, selecting one by one char and update the font for each.
 
I just tested and it worked for me just fine :) but, maybe i am missing something though
It is also documented:
help said:
If the current text selection has more than one font specified, this property is a null reference (Nothing).
So, you were not testing what vblm asked about obviously.
 
Back
Top