C# Windows 窗体中标签的自动换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1204804/
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
Word wrap for a label in Windows Forms
提问by Nagu
How can we get word wrap functionality for a label in Windows Forms?
我们如何在Windows 窗体中获得标签的自动换行功能?
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?
我在面板中放置了一个标签,并添加了一些文本来动态标记。但它超过了面板长度。我该如何解决这个问题?
采纳答案by Jonathan C Dickinson
The quick answer: switch offAutoSize.
快速回答:关闭AutoSize。
The big problem here is that the label will not change its height automatically (only width). To get this right you will need to subclass the label and include vertical resize logic.
这里的大问题是标签不会自动改变其高度(只有宽度)。为了做到这一点,您需要对标签进行子类化并包含垂直调整大小逻辑。
Basically what you need to do in OnPaint is:
基本上你需要在 OnPaint 中做的是:
- Measure the height of the text (Graphics.MeasureString).
- If the label height is not equal to the height of the text set the height and return.
- Draw the text.
- 测量文本的高度 (Graphics.MeasureString)。
- 如果标签高度不等于文本高度,则设置高度并返回。
- 绘制文本。
You will also need to set the ResizeRedrawstyle flag in the constructor.
您还需要在构造函数中设置ResizeRedraw样式标志。
回答by hypo
From MSDN, Automatically Wrap Text in Label:
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 John Gietzen
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.)
如果您将标签设置为 AutoSize,它将随着您放入的任何文本而自动增长。(这包括垂直增长。)
If you want to make it word wrap at a particular width, you can set the MaximumSize property.
如果您想让它以特定宽度自动换行,您可以设置 MaximumSize 属性。
myLabel.MaximumSize = new Size(100, 0);
myLabel.AutoSize = true;
Tested and works.
测试和工作。
回答by Sebastian Castaldi
Bad news: there is not an autowrap property.
坏消息:没有 autowrap 属性。
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
- 选择标签的属性
- 自动调整大小 = 真
MaximumSize = ( Width, Height) 其中Width= 您希望标签的最大尺寸和Height= 您希望它包装的像素数
回答by Sweety Jain
Use style="overflow:Scroll"
in the label as in the below HTML. This will add the scroll bar in the label within the panel.
style="overflow:Scroll"
在标签中使用,如下面的 HTML。这将在面板内的标签中添加滚动条。
<asp:Label
ID="txtAOI"
runat="server"
style="overflow:Scroll"
CssClass="areatext"
BackColor="White"
BorderColor="Gray"
BorderWidth="1"
Width = "900" ></asp:Label>
回答by alex555
In my case (label on a panel) I set label.AutoSize = false
and label.Dock = Fill
.
And the label text is wrapped automatically.
就我而言(面板上的标签)我设置了label.AutoSize = false
和label.Dock = Fill
。并且标签文本会自动换行。
回答by binki
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.
如果您的面板限制了标签的宽度,您可以将标签的 Anchor 属性设置为 Left、Right 并将 AutoSize 设置为 true。这在概念上类似于侦听 Panel 的SizeChanged
事件并将标签的 MaximumSize 更新new Size(((Control)sender).Size.Width, 0)
为之前的答案所建议的a。Anchor 属性中列出的每一面都锚定到包含控件的相应内侧。因此,在 Anchor 中列出两个相对的边有效地设置了控件的维度。锚定到左和右设置控件的宽度属性,锚定到顶部和底部将设置其高度属性。
This solution, as C#:
此解决方案,如 C#:
label.Anchor = AnchorStyles.Left | AnchorStyles.Right;
label.AutoSize = true;
回答by noelicus
- Put the label inside a panel
Handle the
ClientSizeChanged event
for 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); }
Set
Auto-Size
for the label totrue
- Set
Dock
for the label toFill
- 将标签放在面板内
处理
ClientSizeChanged event
面板的 ,使标签填充空间:private void Panel2_ClientSizeChanged(object sender, EventArgs e) { label1.MaximumSize = new Size((sender as Control).ClientSize.Width - label1.Left, 10000); }
将
Auto-Size
标签设置为true
- 将
Dock
标签设置为Fill
回答by Kjell Verbeke
If you really want to set the label width independent of the content, I find that the easiest way is this:
如果你真的想设置独立于内容的标签宽度,我发现最简单的方法是这样的:
- Set autosize true
- Set maximum width to how you want it
- Set minimum width identically
- 设置自动大小为真
- 将最大宽度设置为您想要的
- 设置最小宽度相同
Now the label is of constant width, but it adapts its height automatically.
现在标签具有恒定宽度,但它会自动调整其高度。
Then for dynamic text, decrease the font size. If necessary, use this snippet in the sub where the label text is set:
然后对于动态文本,减小字体大小。如有必要,请在设置标签文本的子中使用此代码段:
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
回答by user3356581
I had to find a quick solution, so I just used a TextBox with those properties:
我必须找到一个快速的解决方案,所以我只使用了具有这些属性的 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
};