如何获得一个标签的换行功能的文本,这是越界?
当前回答
把标签放在面板里 处理面板的ClientSizeChanged事件,使 标签填充空间: Panel2_ClientSizeChanged(对象发送器,EventArgs e) { label1。MaximumSize =新大小((发送方作为控件). clientsize . Size。宽度- label1。离开,10000); } 将标签的自动大小设置为true 将标签“Dock”设置为“Fill”
除了第2步,其他步骤通常都在设计器窗口中完成。
其他回答
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;
我必须找到一个快速的解决方案,所以我只是使用了一个具有这些属性的文本框:
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
};
如果需要保持按钮尺寸不变:
myButton.Text = "word\r\nwrapped"
不确定它是否适合所有用例,但我经常使用一个简单的技巧来获得包装行为: 把你的标签与AutoSize=false在1x1 TableLayoutPanel,这将照顾标签的大小。
把标签放在面板里 处理面板的ClientSizeChanged事件,使 标签填充空间: Panel2_ClientSizeChanged(对象发送器,EventArgs e) { label1。MaximumSize =新大小((发送方作为控件). clientsize . Size。宽度- label1。离开,10000); } 将标签的自动大小设置为true 将标签“Dock”设置为“Fill”
除了第2步,其他步骤通常都在设计器窗口中完成。
推荐文章
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 在c#的控制台应用程序中使用'async
- 在单元测试中设置HttpContext.Current.Session
- 如何开始开发Internet Explorer扩展?
- 更新行,如果它存在,否则插入逻辑实体框架
- 在什么情况下SqlConnection会自动被征召到环境事务范围事务中?
- 用c#解析JSON
- Windows窗体中的标签的换行
- 为什么在c#中使用finally ?
- 为什么不是字符串。空一个常数?
- 为什么我不能在c#中有抽象静态方法?
- Nuget连接尝试失败“无法为源加载服务索引”
- net HttpClient。如何POST字符串值?
- 我如何使一个方法的返回类型泛型?