Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Automatically adjust font size to fully fill Rich Textbox

Posted on 2008-11-10
Medium Priority
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 48

Accepted Solution

Wayne Taylor (webtubbs) earned 2000 total points
ID: 22928066
There is no method like "Autofit". The code you have is the best there is.


Author Comment

ID: 22936361
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

580 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