Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

VB.NET - Change font size to fit all text in a RichTextBox

5.00/5 (4 votes)
19 Dec 2010CPOL 21.7K  
Using VB.NET's RichTextBox and fitting all text into the box
Thanks to the wonderful users on The Code Project (Specifically Kschuler[^]), I was able to create a fairly simple program to resize the text in a RichTextBox to completely fill the Rich Text Box with text. This would be the polar opposite of the AutoSize property. Instead of making the control larger or smaller to fit the text, the text is fitted to the size of the control.

If the text is too long to fit, the font is made smaller, if it is too small the font size is increased. Actually, we are not adjusting the font size, but the ZoomFactor property of the RichTextBox.

To use the below code, create a RichTextBox object (RichTextBox1) and a button (Button1). Fill the RichTextBox with text and run the program. Hitting Button1 will cause the text to be adjusted.

VB
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
    Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Integer) As Integer
    Public Const GWL_STYLE As Integer = (-16)
    Public Const WS_VSCROLL As Integer = &H200000
    Public Const WS_HSCROLL As Integer = &H100000
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Center the text in the RichBox
        RichTextBox1.SelectAll()
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
        'Is scrollbar visible?
        Dim bVScrollBar As Boolean
        bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
        Select Case bVScrollBar
            Case True
                'Scrollbar is visible - Make it smaller
                Do
                    RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor - 0.01
                    bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                    'If the scrollbar is no longer visible we are done!
                    If bVScrollBar = False Then Exit Do
                Loop
            Case False
                'Scrollbar is not visible - Make it bigger
                Do
                    RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor + 0.01
                    bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                    If bVScrollBar = True Then
                        Do
                            'Found the scrollbar, make smaller until bar is not visible
                            RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor - 0.01
                            bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                            'If the scrollbar is no longer visible we are done!
                            If bVScrollBar = False Then Exit Do
                        Loop
                        Exit Do
                    End If
                Loop
        End Select
    End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)