Link to home
Start Free TrialLog in
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)Flag for Australia

asked on

Change Font in RichTextBox

Hi!

I have a RichTextBox that has different font styles. Some of the text is Bold, some is a Different color, some is both, and the rest is regular.

But how can I change only the font name and the size of the entire RTB, while retaining the bold and color of on some of the text?

Cheers,

Wayne
Avatar of Sancler
Sancler

Try this

    Private Sub ChangeFontPreserveFormatting()

        Dim oldFontName, newFontName As String

        oldFontName = RichTextBox1.Font.FontFamily.Name

        If FontDialog1.ShowDialog() <> DialogResult.Cancel Then
            Try
                newFontName = FontDialog1.Font.FontFamily.Name
                RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(oldFontName, newFontName)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub

Roger
This is better, as it allows you to change the whole font more than once

    Private Sub ChangeFontPreserveFormatting()

        Static oldFontName As String
        Static newFontName As String

        If oldFontName = "" Then
            oldFontName = RichTextBox1.Font.FontFamily.Name
        Else
            oldFontName = newFontName
        End If

        If FontDialog1.ShowDialog() <> DialogResult.Cancel Then
            Try
                newFontName = FontDialog1.Font.FontFamily.Name
                RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(oldFontName, newFontName)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub

Roger
Avatar of Wayne Taylor (webtubbs)

ASKER

Thanks Roger, that's got the font name, but how do I do the size?
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Roger. Worked a treat :)