如何获得一个标签的换行功能的文本,这是越界?
当前回答
我必须找到一个快速的解决方案,所以我只是使用了一个具有这些属性的文本框:
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
};
其他回答
我有一个标签,自动包装和增长到任何大小在右停靠的自动大小面板,其宽度是由其他内容设置。
标签(在tablelayoutpanel) Autosize = True。
tableelayoutpanel(在面板)Autosize = True, AutoSizeMode = GrowAndShrink, Dock =底部,一列SizeType = 100%,一行SizeType = 100%。
面板(右停靠形式)AutoSize = True, AutoSizeMode = GrowAndShrink, Dock =右。
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;
这帮助了我的表格InpitWindow: 标签设计师:
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);
}
我身体的全部代码
使用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步,其他步骤通常都在设计器窗口中完成。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本