Automatically adjust font size to fully fill Rich Textbox

Posted on 2008-11-10
Last Modified: 2012-08-14
In Visual Basic 6 we use the GetFirstVisible API to check if all the text in a Textbox is visible. We then decrease the font size until all text is visible.

We are looking to accomplish the same thing in Visual Basic 2008, in a Rich Textbox.

Attached is the Procedure we use to check if the font is too big to fit in the text box. If the procedure returns "True" then we make the font smaller and we call the Procedure again.

We would really prefer something like an "AutoFit" feature (like labels have), if this is possible.
Public Function FontTooBig(obj As Object) As Boolean

    Dim WindowHandle As Long

    WindowHandle = obj.hwnd

    obj.Selection.Range.StartPos = Len(obj.Text)

    LinesNotVisible = SendMessage(WindowHandle, EM_GETFIRSTVISIBLELINE, 0, 0)

    If LinesNotVisible <> 0 Then

        FontTooBig = True


        FontTooBig = False

    End If

End Function

Open in new window

Question by:esmyhs
    LVL 46

    Accepted Solution

    There is no method like "Autofit". The code you have is the best there is.


    Author Comment

    Thanks for the response. The problem that we are having with the attached code is that we get an error message on the following line:

    WindowHandle = obj.hwnd

    We get the following error message:

    System.MissingMemberException was unhandled
      Message="Public member 'hwnd' on type 'RichTextBox' not found."

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now