C# 标签自动换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9509147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:41:28  来源:igfitidea点击:

Label word wrapping

c#winformsc#-4.0c#-3.0

提问by Sharpeye500

Is there a way to do a word wrap in a .NETlabel control?

有没有办法在.NET标签控件中进行自动换行?

I know there is an alternate way of using a TextBox, make property BorderStyleto none, property ReadOnlyto true and set property WordWrapand property Multilineto true.

我知道有另一种使用TextBox 的方法,将属性BorderStyle 设置为 none,将属性ReadOnly设置为 true 并将属性WordWrap和属性Multiline 设置为 true。

Is there something for a label?

有标签的东西吗?

采纳答案by mindandmedia

Refer to Automatically Wrap Text in Label. It describes how to create your own growing label.

请参阅在标签中自动换行文本。它描述了如何创建自己的成长标签。

Here is the full source taken from the above reference:

以下是上述参考资料的完整来源:

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

public class GrowLabel : Label {
  private bool mGrowing;
  public GrowLabel() {
    this.AutoSize = false;
  }
  private void resizeLabel() {
    if (mGrowing) return;
    try {
      mGrowing = true;
      Size sz = new Size(this.Width, Int32.MaxValue);
      sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
      this.Height = sz.Height;
    }
    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();
  }
}

回答by fa wildchild

Change your maximum size,

改变你的最大尺寸,

label1.MaximumSize = new Size(100, 0);

And set your autosize to true.

并将您的自动调整大小设置为 true。

label1.AutoSize = true;

That's it!

就是这样!

回答by atconway

Ironically, turning off AutoSizeby setting it to falseallowed me to get the label control dimensions to size it both verticallyand horizontally which effectively allows word-wrapping to occur.

具有讽刺意味的是,AutoSize通过将其设置为关闭,false我可以让标签控件尺寸在垂直和水平方向上调整其大小,从而有效地允许自动换行。

回答by Abdullah Alhutami

Just set Label AutoSize property to False. Then the text will be wrapped and you can re-size the control manually to show the text.

只需将 Label AutoSize 属性设置为 False。然后文本将被换行,您可以手动重新调整控件的大小以显示文本。

回答by noelicus

If you want some dynamic sizing in conjunction with a word-wrapping label you can do the following:

如果您想要一些动态大小与自动换行标签一起使用,您可以执行以下操作:

  1. Put the label inside a panel
  2. Handle the ClientSizeChanged eventfor the panel, making the label fill the space:

    private void Panel2_ClientSizeChanged(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size((sender as Control).ClientSize.Width - label1.Left, 10000);
    }
    
  3. Set Auto-Sizefor the label to true

  4. Set Dockfor the label to Fill
  1. 将标签放在面板内
  2. 处理ClientSizeChanged event面板的 ,使标签填充空间:

    private void Panel2_ClientSizeChanged(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size((sender as Control).ClientSize.Width - label1.Left, 10000);
    }
    
  3. Auto-Size标签设置为true

  4. Dock标签设置为Fill

回答by Jurgen Camilleri

If you open the dropdown for the Textproperty in Visual Studio, you can use the enter key to split lines. This will obviously only work for static text unless you know the maximum dimensions of dynamic text.

如果Text在 Visual Studio 中打开属性的下拉列表,则可以使用 Enter 键来拆分行。这显然只适用于静态文本,除非您知道动态文本的最大尺寸。

回答by mLar

You can use a TextBoxand set multilineto trueand canEditto false.

您可以使用 a TextBoxand 设置multilinetrueand canEditto false