Automatically Set the Height on a Fixed-Width UltraLabel

Jason Beres / Saturday, February 28, 2009

If you set AutoSize = true on an UltraLabel, its height becomes fixed and its width expands/collapses to accommodate its text.  So how can you fix its width and have the UltraLabel autosize its height to accommodate the height of the wrapped text?

Since the control doesn't natively support this, you can derive a control from UltraLabel and override the constructor and set AutoSizeHeightOnly to true.

class UltraLabelWithAutoHeight : UltraLabel
{
    public UltraLabelWithAutoHeight()
        : base()
    {
        this.AutoSizeHeightOnly = true;
    }
}

Note that the designer will not let you change the Width of the control once AutoSize is set to true. So this is a little inconvenient to work with at design-time, but it is easier than measuring the text yourself.