如何获得一个标签的换行功能的文本,这是越界?


当前回答

我有一个标签,自动包装和增长到任何大小在右停靠的自动大小面板,其宽度是由其他内容设置。

标签(在tablelayoutpanel) Autosize = True。

tableelayoutpanel(在面板)Autosize = True, AutoSizeMode = GrowAndShrink, Dock =底部,一列SizeType = 100%,一行SizeType = 100%。

面板(右停靠形式)AutoSize = True, AutoSizeMode = GrowAndShrink, Dock =右。

其他回答

我有一个标签,自动包装和增长到任何大小在右停靠的自动大小面板,其宽度是由其他内容设置。

标签(在tablelayoutpanel) Autosize = True。

tableelayoutpanel(在面板)Autosize = True, AutoSizeMode = GrowAndShrink, Dock =底部,一列SizeType = 100%,一行SizeType = 100%。

面板(右停靠形式)AutoSize = True, AutoSizeMode = GrowAndShrink, Dock =右。

没有自动抓取属性,但可以通过编程来动态调整大小。这里有一个解决方案:

选择标签的属性 AutoSize = True MaximumSize =(宽度,高度)其中宽度=你想要标签的最大尺寸和高度=你想要它包装多少像素

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

设置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

根据@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.Parent.Width;这允许你使用自动增长标签时停靠到一个父,例如一个面板。

这一点。高度= sz。高度+填充。Bottom + Padding.Top;这里我们关心顶部和底部的填充。

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

myButton.Text = "word\r\nwrapped"