user3613916
user3613916

Reputation: 232

Showing ellipsis in GroupBox, when text is too long?

RadioButton and CheckBox have WrapText property. When it is enabled, a text, which is too long for a control, gets wrapped. When it is disabled, ellipsis appears at the end.

Label has AutoElipsis property that does the same thing, except it has to be enabled for ellipsis to show.

How to force the same behavior in GroupBox?

Upvotes: 0

Views: 592

Answers (1)

msmolcic
msmolcic

Reputation: 6557

There you go, I created custom control for you. Use it instead of GroupBox, you can also enable/disable AutoElipsis through designer properties or in code.

public class ElipsisGroupBox : GroupBox
{
    private string _tempText;
    private bool _isElipsisOn;

    public ElipsisGroupBox () : base()
    {
        _tempText = string.Empty;
        _isElipsisOn = false;
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        if (this.AutoElipsis)
        {
            _tempText = base.Text;
            if (_tempText.Length == 0 || base.Width == 0)
                return;

            int i = _tempText.Length;

            string textToCheck = _isElipsisOn ? _tempText : _tempText + "...";

            _isElipsisOn = false;
            while (TextRenderer.MeasureText(textToCheck, base.Font).Width > (base.Width - 14))
            {
                _isElipsisOn = true;
                textToCheck = base.Text.Substring(0, --i) + "...";
                if (i == 0)
                    break;
            }

            if (_isElipsisOn)
                _tempText = textToCheck;
        }
    }

    public override string Text
    {
        get
        {
            return this.AutoElipsis && _isElipsisOn ? _tempText : base.Text;
        }
        set
        {
            if (this.AutoElipsis && _isElipsisOn)
                _tempText = value;
            else
                base.Text = value;
        }
    }

    public bool AutoElipsis { get; set; }
}

Upvotes: 2

Related Questions