Word wrap for a label in Windows Forms

2019-01-01 15:13发布

How can we get word wrap functionality for a label in Windows Forms?

I placed a label in a panel and added some text to label dynamically. But it exceeds the panel length. How can I solve this?

16条回答
谁念西风独自凉
2楼-- · 2019-01-01 15:29

Actually, the accepted answer is unnecessarily complicated.

If you set the label to AutoSize, it will automatically grow with whatever text you put in it. (This includes vertical growth.)

If you want to make it word wrap at a particular width, you can set the MaximumSize property.

myLabel.MaximumSize = new Size(100, 0);
myLabel.AutoSize = true;

Tested and works.

查看更多
浅入江南
3楼-- · 2019-01-01 15:34

Bad news: there is not an autowrap property.

Good news: there is a light at the end of the tunnel!

You could accomplish this programmatically to size it dynamically, but here is the easiest solution:

  • Select the properties of the label
  • AutoSize = True
  • MaximumSize = (Width, Height) where Width = max size you want the label to be and Height = how many pixels you want it to wrap

    Sample Properties

查看更多
骚的不知所云
4楼-- · 2019-01-01 15:35

Not sure it will fit all use-cases but I often use a simple trick to get the wrapping behaviour: put your Label with AutoSize=false inside a 1x1 TableLayoutPanel which will take care of the Label's size.

查看更多
其实,你不懂
5楼-- · 2019-01-01 15:36

If your panel is limiting the width of your label, you can set your label’s Anchor property to Left, Right and set AutoSize to true. This is conceptually similar to listening for the Panel’s SizeChanged event and updating the label’s MaximumSize to a new Size(((Control)sender).Size.Width, 0) as suggested by a previous answer. Every side listed in the Anchor property is, well, anchored to the containing Control’s respective inner side. So listing two opposite sides in Anchor effectively sets the control’s dimension. Anchoring to Left and Right sets the Control’s Width property and Anchoring to Top and Bottom would set its Height property.

This solution, as C#:

label.Anchor = AnchorStyles.Left | AnchorStyles.Right;
label.AutoSize = true;
查看更多
闭嘴吧你
6楼-- · 2019-01-01 15:36

If the dimensions of the button need to be kept unchanged:

myButton.Text = "word\r\nwrapped"
查看更多
柔情千种
7楼-- · 2019-01-01 15:39

Have a better one based on @hypo 's answer

public class GrowLabel : Label {
    private bool mGrowing;
    public GrowLabel() {
        this.AutoSize = false;
    }
    private void resizeLabel() {
        if (mGrowing)
            return;
        try {
            mGrowing = true;
            int width = this.Parent == null ? this.Width : this.Parent.Width;

            Size sz = new Size(this.Width, Int32.MaxValue);
            sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
            this.Height = sz.Height + Padding.Bottom + Padding.Top;
        } finally {
            mGrowing = false;
        }
    }
    protected override void OnTextChanged(EventArgs e) {
        base.OnTextChanged(e);
        resizeLabel();
    }
    protected override void OnFontChanged(EventArgs e) {
        base.OnFontChanged(e);
        resizeLabel();
    }
    protected override void OnSizeChanged(EventArgs e) {
        base.OnSizeChanged(e);
        resizeLabel();
    }
}

int width = this.Parent == null ? this.Width : this.Parent.Width; this allows you to use auto-grow label when docked to a parent, e.g. a panel.

this.Height = sz.Height + Padding.Bottom + Padding.Top; here we take care of padding for top and bottom.

查看更多
登录 后发表回答