-1

In my C# WinForms application, I have a control in which I display some text to the user on screen. For time being, assume it is a TextBox.

My requirement is if the text does not fully fit within the displayed width of the control, I want to keep reducing the font size or compress the text in some other way to fit the displayed width of the control.

I understand in extreme situations, the text may not be readable at all. But that's fine.

Can I get a code example how to achieve this?

AllSolutions
  • 1,176
  • 5
  • 19
  • 40

1 Answers1

2

To measure the width of the font you'll have to determine it using TextRenderer. The following code illustrates how to achieve this, and to resize the font in the textbox.

var text = "Some unnecessarily long, long, long string.";

var size = default(SizeF);
// SizeF size; // Use this if you're on an older version of C# without default


do
{
    using (var font = new Font(textBox1.Font.Name, textBox1.Font.SizeInPoints))
    {
        size = TextRenderer.MeasureText(text, font);

        if (size.Width <= textBox1.Width)
            textBox1.Text = text;
        else
        {
            textBox1.Text = "Won't fit";
            textBox1.Font = new Font(font.Name, font.SizeInPoints - 1f);
        }
    }
} while (size.Width > textBox1.Width);

You may want to adjust the by how much the font size decreases if it ends up too small for your liking.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
  • 3
    For WinForms controls, [you should use `TextRenderer.MeasureText()`, not `Graphics.MeasureString()`](https://stackoverflow.com/a/6705023/4934172). – 41686d6564 stands w. Palestine Jul 28 '18 at 14:48
  • Is there any performance penalty if there are lots of textboxes in the form? And is there a professional third party suite of controls which support such a feature of auto-shrinking text to fit the displayed width of the control? – AllSolutions Jul 28 '18 at 14:48
  • @AhmedAbdelhameed good catch. Thanks for the suggestion, I've updated the code – Fabulous Jul 28 '18 at 14:51
  • @AllSolutions I'm unaware of any such tools, and you'll have to look beyond stack overflow for those. The performance degradation will depend on how many **lots** refers to. Is this text supposed to go into all textboxes, and are they the same size. If so, calculate for one and then apply the same font settings to all of them. – Fabulous Jul 28 '18 at 14:54
  • Nice answer! Just remember to dispose the `font` variable at the end of the loop, otherwise you'll get memory leaks. – Visual Vincent Jul 28 '18 at 14:57
  • @Fabulous, regarding third party suites of controls, on which Stack Exchange site can I post such a question? There is a site "Software Recommendations", but not sure if it will get any answers for programming related third party tools. – AllSolutions Jul 28 '18 at 15:01
  • @AllSolutions I've never used the Software Recommendations one so I cannot assist further than the code here. – Fabulous Jul 28 '18 at 15:06
  • 1
    This is not tested code, there are many problems with it. At least look at how the [framework code does it](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/TextBoxBase.cs,fec237b930cfc431). – Hans Passant Jul 28 '18 at 15:07
  • Thanks @HansPassant. – AllSolutions Jul 28 '18 at 15:12
  • All, if possible, respond to this thread: https://softwarerecs.stackexchange.com/questions/51322/c-winforms-grid-control-supporting-layout-of-rows-of-controls – AllSolutions Jul 28 '18 at 15:13
  • Thanks for the feedback @HansPassant. I'm looking at that code though and at first glance it seems to be calculating the size of the control and not the contained text. I'll look into it further and make necessary adjustments. – Fabulous Jul 28 '18 at 15:25