Click here to Skip to main content
Click here to Skip to main content

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

, 19 Dec 2010
Rate this:
Please Sign up or sign in to vote.
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.
 
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)

About the Author

VyReN

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 19 Dec 2010
Article Copyright 2010 by VyReN
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid