我们如何可以获得一词包功能的标签 Windows形式?

我放置的标签,在一个面板,并加入一些文字来标记的动态。但是,它超过了小组的长度。我如何能解决这个?

有帮助吗?

解决方案

快速回答:切换关闭 AutoSize

这里的一个大问题是标签不会自动改变其高度(仅宽度)。为了实现这一目标,您需要对标签进行子类化并包含垂直调整大小逻辑。

基本上你在OnPaint中需要做的是:

  1. 测量文本的高度(Graphics.MeasureString)。
  2. 如果标签高度不等于文本的高度,则设置高度并返回。
  3. 绘制文字。
  4. 您还需要设置 ResizeRedraw 样式标志。

其他提示

实际上,接受的答案是不必要的复杂。

如果您将标签设置为AutoSize,它将随您放入的任何文本自动增长。 (这包括垂直增长。)

如果要使其以特定宽度进行自动换行,可以设置MaximumSize属性。

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

经过测试和工作。

在我的情况下(面板上的标签)我设置 label.AutoSize = false label.Dock = Fill 。 标签文本会自动换行。

坏消息:没有一个autowrap财产。

好消息:有一个光隧道的尽头!

你可以完成这一程大小的动态,但这里是最简单的解决方案:

  • 选择性标签
  • 自动调整大小=True
  • MaximumSize=(, 高度)在那里 =max你想要的尺寸的标签以及 高度 =多少素你想要它来包裹

    Sample Properties

MSDN 自动换行标签中的文字

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();
    }
}

我必须找到一个快速的解决方案,所以我只使用了带有这些属性的TextBox:

var myLabel = new TextBox
                    {
                        Text = "xxx xxx xxx",
                        WordWrap = true,
                        AutoSize = false,
                        Enabled = false,
                        Size = new Size(60, 30),
                        BorderStyle = BorderStyle.None,
                        Multiline =  true,
                        BackColor =  container.BackColor
                    };

根据@hypo的回答有一个更好的答案

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.Height = sz.Height + Padding.Bottom + Padding.Top; 这里我们处理顶部和底部的填充。

  1. 将标签放在面板中
  2. 处理面板的 ClientSizeChanged事件, 标签填充空间:

    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

不确定它是否适合所有用例但我经常使用一个简单的技巧来获取包装行为: 将 Label AutoSize = false 放在1x1 TableLayoutPanel 中,它将处理 Label 的大小。

将AutoEllipsis属性设置为“TRUE”,将AutoSize属性设置为“FALSE”。

如果您的面板限制了标签的宽度,您可以将标签’的Anchor属性设置为Left,Right并将AutoSize设置为true。这在概念上类似于侦听Panel’ SizeChanged 事件并将标签’的MaximumSize更新为 new Size(((Control)sender).Size.Width, 0),如之前的答案所示。 Anchor属性中列出的每一方都固定在包含Control的相应内侧。因此,在Anchor中列出两个相反的方面可以有效地设置控件的维度。锚定到Left和Right设置Control’ s Width属性,Anchoring to Top和Bottom将设置其Height属性。

此解决方案,如C#:

label.Anchor = AnchorStyles.Left | AnchorStyles.Right;
label.AutoSize = true;

如果你真的想要设置标签宽度的独立的内容,我发现,最简单的方法是这样的:

  • 设置自动调整大小真的
  • 设定的最大宽你怎么想要它
  • 设定最小宽度相同

现在标签的宽度不变,但它可以适应它的高度。

然后动态的文本,减少的字体大小。如果有必要,使用这个段子标签的案文是设定:

If Me.Size.Height - (Label12.Location.Y + Label12.Height) < 20 Then
    Dim naam As String = Label12.Font.Name
    Dim size As Single = Label12.Font.SizeInPoints - 1
    Label12.Font = New Font(naam, size)
End If

这在我的名为InpitWindow的表格中帮助了我: 在Label for Label中:

AutoSize = true;
Achors = Top, Left, Right.

private void InputWindow_Shown(object sender, EventArgs e) {
    lbCaption.MaximumSize = new Size(this.ClientSize.Width - btOK.Width - btOK.Margin.Left - btOK.Margin.Right -
        lbCaption.Margin.Right - lbCaption.Margin.Left, 
        Screen.GetWorkingArea(this).Height / 2);
    this.Height = this.Height + (lbCaption.Height - btOK.Height - btCancel.Height);
    //Uncomment this line to prevent form height chage to values lower than initial height
    //this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);
}
//Use this handler if you want your label change it size according to form clientsize.
private void InputWindow_ClientSizeChanged(object sender, EventArgs e) {
    lbCaption.MaximumSize = new Size(this.ClientSize.Width - btOK.Width - btOK.Margin.Left * 2 - btOK.Margin.Right * 2 -
        lbCaption.Margin.Right * 2 - lbCaption.Margin.Left * 2,
        Screen.GetWorkingArea(this).Height / 2);
}

表格的整码

如果按钮的尺寸需要保持不变:

myButton.Text = "word\r\nwrapped"

此问题的简单答案是更改Label的DOCK属性。它是“无”的。默认情况下。

在标签中使用 style =&quot; overflow:Scroll&quot; ,如下面的HTML所示。这将在面板中的标签中添加滚动条。

<asp:Label
    ID="txtAOI"
    runat="server"
    style="overflow:Scroll"
    CssClass="areatext"
    BackColor="White"
    BorderColor="Gray"
    BorderWidth="1"
    Width = "900" ></asp:Label>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top