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


当前回答

使用System.Windows.Forms.LinkLabel代替Label,并设置属性LinkArea如下所示。

myLabel.LinkArea = new LinkArea(0, 0);

其他回答

把标签放在面板里 处理面板的ClientSizeChanged事件,使 标签填充空间: Panel2_ClientSizeChanged(对象发送器,EventArgs e) { label1。MaximumSize =新大小((发送方作为控件). clientsize . Size。宽度- label1。离开,10000); } 将标签的自动大小设置为true 将标签“Dock”设置为“Fill”

除了第2步,其他步骤通常都在设计器窗口中完成。

事实上,公认的答案是不必要的复杂。

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

如果你想让它以特定的宽度换行,你可以设置MaximumSize属性。

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

测试和工作。

这个问题的简单答案是改变Label的DOCK属性。默认为“NONE”。

使用System.Windows.Forms.LinkLabel代替Label,并设置属性LinkArea如下所示。

myLabel.LinkArea = new LinkArea(0, 0);

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.

这个解决方案,作为c#:

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