Skip to content

Scenarios for sizing the font in a TextBox?

June 13, 2007

A few of us are discussing TextBox font sizing scenarios.  Would love your input.  I found some code on the VB.NET forum showing text resizing for a TextBox.  It seems a bit strange because of the MaxSize aspect of the MakeFit routine he writes.  What kind of FontSize control do you want for TextBox scenarios?  Have you been able to do what you need to?

Mail thread follows, please read bottom-up:

From: Shawn Van Ness

The original poster’s requirements are probably fairly unusual…  I suspect, by far, the more common scenario is to want to set the FontSize based (solely) on the TextBox’s height.

Is there a readymade way to do that?  Or a simple formula…  something like

double effectiveHeight = textbox.ActualHeight – textbox.Padding.Top – textbox.Padding.Bottom –

    textbox.BorderThickness.Top – textbox.BorderThickness.Bottom;

textbox.FontSize = effectiveHeight * 72d / 96d;

From: Rob Relyea

Not sure how common this scenario is…but it is interesting to see what our users are trying to do and how they have to do it (this code is in VB):

[originally from: Code Example: Automatically resize text to fit textbox]

This one drove me crazy for a while, I was used to being able to do this using measuretext when using GDI.
(ONLY suitable for single line textboxes)


TextBox1.FontSize = MakeFit(TextBox1,11)



Function MakeFit(ByVal textbox As TextBox, Optional ByVal MaxFontSize As Double = 22) As Double

        Dim FontSize As Double = MaxFontSize

        textbox.FontSize = FontSize

        Dim TextRectangle As Rect = textbox.GetRectFromCharacterIndex(textbox.Text.Length)

        Dim width As Double = TextRectangle.Right

        Dim height As Double = TextRectangle.Bottom

        Dim MaxWidth As Double = textbox.ActualWidth – (textbox.Padding.Right + textbox.Padding.Left)

        Dim MaxHeight As Double = (textbox.ActualHeight – (textbox.Padding.Top + textbox.Padding.Bottom))

        Do Until width < MaxWidth And height < MaxHeight

            FontSize -= 1

            textbox.FontSize = FontSize

            TextRectangle = textbox.GetRectFromCharacterIndex(textbox.Text.Length)

            width = TextRectangle.Right

            height = TextRectangle.Bottom


        Return FontSize

    End Function

From → WPF

  1. John permalink

    Hi. I posted the original code in the forum.The reason behind the maxsize property, is that you\’ll notice the code loops to find the best size for that textbox.In my usage scenario, this code could be called several times a second, and so I was looking for extra speed.  Without the maxsize, then the code would have to loop from the lergest imaginable size (e.g. 128pt) and loop downwards until it fits.However, with maxsize in place, the code starts from that size and works downwards, so as you can see, it can be a considerable timesaver.

  2. John permalink

    Shawn: My primary concern was actually the width of the text. In my usage scenario, the text changes in that textbox quite a lot, but I cannot afford the space for it to wrap. Which is why now I can have a textbox on a fork, call makefit on the textchanged event, and the text will resize if necessary.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: