Geeks With Blogs
Brian Biales because blogging is just the easiest way to remember things
Most resizing logic is either geared toward running the application in different resolutions, or in keeping a control proportionately placed on a page or with reference to other controls.  The Dock feature is great for handling a lot of your resizing needs as well.  But I've found a solution to a different resizing problem that I'll bet others have run into as well.
Let’s say I have placed three labels (or other controls) in a strip at the top of the screen, and these are resized to each be 1/3 the width of the window. But I want to make sure that the text in each (which may vary in length) remains visible in its entirety no matter what the user does to resize the window. My solution is to vary the font size when the control is resized. And I found it pretty easy to figure out what font size will allow the text to fit.  I use the preferredWidth() method (some controls use preferredSize(), but the Label uses preferredWidth) to figure out if the text inside would "prefer" to be in a control that is wider than this one.  If so, I drop the font size down a bit and test again.  Once it fits, we're done.
This is not perfect, you may need to alter it slightly for your needs. For instance, I allow the height to grow as needed if the font is now bigger. If this doesn’t work for you, you’ll have to ensure it is always tall enough for the largest font that will be used. Also, I’ve found that when using CAB Workspaces, sometimes I need to resize the height of the parent Workspace myself when the SmartPart changes size.  That could be another article some day...
Here is the code, I hope you find it useful! 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AlwaysFitLabel
The Label that always displays its text
    public class AlwaysFitLabel :
        private float _maxFontSize = 16F;
// Pick a default value...
        public float maxFontSize { get { return _maxFontSize; } set { _maxFontSize = value; } }

        public AlwaysFitLabel()
// Add a handler for the resize event, which we implement below
            this.Resize += new EventHandler(AlwaysFitLabel_Resize);
        public override string Text
                return base.Text;
// always resize it when text changes...
                base.Text = value;
                AlwaysFitLabel_Resize(this, new EventArgs());
        void AlwaysFitLabel_Resize(object sender, EventArgs e)
// This function uses a temporary label to put the text with the largest font size,
// see if it fits, and if not, keep knocking the font size down by .5 until it does
// fit.  Then set the real label's font size to this newly calculated font size.
            Label lbl = new Label();
            float fntSize = maxFontSize;
            lbl.Text = this.Text;
            lbl.Font = new System.Drawing.Font(this.Font.FontFamily, fntSize);
            while (lbl.PreferredWidth > this.Width && fntSize > 0.6F)
                fntSize -= 0.5F;
                lbl.Font = new System.Drawing.Font(this.Font.FontFamily, fntSize);
            this.Font = new System.Drawing.Font(this.Font.FontFamily, fntSize);
            this.Height = this.PreferredHeight;

Posted on Wednesday, June 13, 2007 11:13 PM .NET , SCSF - Smart Client Software Factory , C# , Visual Studio 2005 | Back to top

Comments on this post: The other way to resize - resize the contents, not the control

# re: The other way to resize - resize the contents, not the control
Requesting Gravatar...
your code does not work if you want multi-line.
Left by J on Oct 24, 2007 3:38 PM

Your comment:
 (will show your gravatar)

Copyright © Brian Biales | Powered by: