在TableLayoutPannel中放着一些Label
如果把Label的AutoSize属性设成True的话,文字超过label长度时就会自动增加,直到后面的字出窗体以外
设置成False时,一旦到达Label的长度,后面的字符也就显示不出来了
经过我的多番实践,最佳的解决方法是
把Label的Dock属性设置成Fill,并同时把Label的AutoSize属性设成False。
以上只是一种简便的解决方法,如果以上方法解决不了问题,就老老实实计算控件大小以适应文本吧。
-----------------------------------------------------------------
具体方法:
C# WinForm中的Label,Button等控件在布局上和Web Application中不一样。
在WebApplication中,你可以指定它们的Width属性,然后当在指定Width内显示不全时,就自动换行,自动增加其Height 属性。
在WinForm中系统不会替你做这些事情。系统要求你必须同时指定Width和Height属性,缺一不可。当一行显示完而高度不足以显示第二行时,控件上的字符就会被截断。后面的字符就不会被显示出来了。
要实现WinForm中类似于WebApp的文本自动换行功能,你就必须手动编程设置控件的高度Height。在把控件添加进Form之前,应先获得控件控件显示文本的字数sumChar=Control.Text.Length,根据字数计算出需要多少行rowCount=(numChar/每行显示字数)+1 (注意:因为当不满一行时,(int)(numChar/每行显示字数)=0,因此必须再加一),那么控件的高度就是Control.Height=rowCount*每行文本的高度
在添加控件进Form之前,加入Control.Size = new Size (控件宽度,计算出来的控件高度)
OK。
应当注意的是,由于中英文以及各种符号的宽度不一致,所以每行显示的字数很难精确计算出来。可以根据显示内容以及经验,确定一个平均值,并且在完成之后多调试,最终确定一个合适的值。
-------------------------------------------------------------------------------
1.单行完全显示:Label.AutoSize = true;
2.换行显示:Label. AutoSize = false;(Label框高度用户指定)。
3.多行显示,并且根据字数自动控制高度:Label.AutoSize = true;Label.MaximumSize = new Size(w,0); 注:w:用户设定的宽度。
------------------------------------------------------
label自动换行的折衷方案:
核心关键在于利用TextBox的MultiLine自动换行功能,实现自动换行,其他就是颜色的设置、高度及所属窗体高度的自动调整。
采用TextBox来实现文字的自动换行,textBox背景色设置为:Control色,AutoSize = true;MultiLize = true;Height = 12;
每个字符占用的宽度大约是12;
然后添加以下代码:
int LblNum = ConfStr.Length; //TextBox内容长度
int RowNum = (int) txtBoxSp.Width/12; //每行显示的字数(计算出来的)
int RowHeight = 12; //每行的高度
int ColNum = (LblNum - (LblNum / RowNum) * RowNum) == 0 ? (LblNum / RowNum) : (LblNum / RowNum) + 1; //列数

if(ColNum == 1)
{
this.Height = 278; //禁止窗体显示textBox;
this.AutoSize = false;
}
else
{
txtBoxSp.AutoSize = true; //设置AutoSize
txtBoxSp.Height = RowHeight * ColNum; //设置显示高度
this.Height = 303 + txtBoxSp.Height + 6; //实现窗体高度的自动调整
}
------------------------------------------------------
The fundamental problem in 1.1
When label is set to AutoSize = true, it measures as if all the text wants to be on one line. In order to have multiple lines of text in 1.1, the AutoSize property cannot be used.
Guessing in 1.1
When you drop a label on a form in version 1.0/1.1 it is set to AutoSize = false. The label can be resized to force the text onto the next line based upon the width of the control. However, guessing the right height for the label can be problematic – if it is not created tall enough, the text will cut off at the bottom. If it’s too tall there will be embarrassing gaps of whitespace in the dialog. To solve this, folks typically used Graphics.MeasureString() with the width the label was currently set to, then used the resultant height to set the size of the label.
There’s all kinds of problems with the latter approach – it doesn’t take into account several extra spacing/non-client borders of the control, it’s not good for performance (if you’re using Control.CreateGraphics() can force the label’s handle to be created before it normally would, etc). …and finally if you upgrade in 2.0 to UseCompatibleTextRendering = false, the calculations will be wrong.
APIs to avoid Guessing in 2.0
The Label control now has a GetPreferredSize() method which takes wrapping constraints. This method will take out all the headaches of guessing by taking into account all the details you might not know about, e.g. non-client borders/padding and the technology used to draw text.
Having your cake and eating it too: Strategies for Automatic Word Wrap in 2.0
It was difficult to get this right in previous versions of Windows Forms, as there was no concept of constraining size. Updating label to support multiline constraints was a delicate balancing act as we did not want to break folks using 1.1. (A 1.1 app should *just work* running on 2.0). If using the regular layout stuff (i.e. not flow and table layout panels) the label should continue to work as before.

The easiest way of supporting multiline text just using “dock and anchor” layout engine was to honor a new property called MaximumSize. This gives the Label a constraining width by suggesting that the maximum width you can be is “xxxx”. When a label is AutoSize = true, it takes into account its MaximumSize when calculating its PreferredSize.
The pitfall to using MaximumSize is that the size of the label is now fixed: if you want the label to increase in width as the dialog increases, you need to write code to increase the MaximumSize. While this is not difficult, it would be better if we could write less code.
One possibility for fixing the Label’s MaximumSize quandary is to place the label in a FlowLayoutPanel. When a FlowLayoutPanel is anchored left|right, it has a constraining width, which it passes onto the label. Setting the FlowLayoutPanel to AutoSize = true, the FlowLayoutPanel will grow in height as the label grows. (The label actually had a constraining width as well when anchored, but for compatibility reasons chose to ignore it.) Because the label is in a new layout container, it is free to honor the wrapping constraints without the possibility of breaking anyone. As the dialog is resized, the FlowLayoutPanel is resized, which in-turn passes a new constraint to the label.
Now that we have the label dynamically changing height with respect to the width of the dialog, we have another problem to solve. If there is another control directly underneath the label, the label can obscure the control directly underneath it. We need to find a way to push the other controls down when the label grows in height.
We could add the control below it to the FlowLayoutPanel we’ve just added, but if we want finer control of the sizing relationship, the situation calls for a TableLayoutPanel. Controlling sizing behavior in TableLayoutPanel means controlling the ColumnStyles and RowStyles. There are three kinds of ColumnStyles and RowStyles in the TableLayoutPanel: Percentage, Absolute and AutoSize.
Briefly: When the TableLayoutPanel control arranges its columns, it assigns priorities to each ColumnStyle in the following order:
1. Columns with ColumnStyle set to Absolute are considered first, and their fixed widths are allocated.
2. Columns with ColumnStyle set to AutoSize are sized to their contents.
3. Remaining space is divided among columns with ColumnStyle set to Percent.
By placing a label within a column that has specific a sizing behavior, the label will wrap. When the label wraps, controls in rows below it will be pushed down if the RowStyle is set to AutoSize.
Here’s a quick table of the behavior of labels within a TableLayoutPanel (TLP) and why they behave that way.
TLP AutoSize
TLP ColumnStyle
Will Label Wrap?
Why?
True and False
Absolute
Yes
Known constraints
Since the column is absolute, we have a known dimension to pass to the label as a wrapping distance.
True and False
AutoSize
No
Unknown constraints
Setting the column to AutoSize implies that we don’t understand currently what the size of the column should be. Therefore all AutoSize = true controls are asked, “given infinite space, what size would you prefer to be?”
False
Percentage
Yes
Known constraints
Since the table is AutoSize = false, we understand that the % style column is a percentage of the remaining space in the table. Therefore we have a known dimension to pass to the label as a wrapping distance.
True
Percentage
No
Unknown constraints
Since the table is AutoSize = true, we don’t understand what % should mean, as in an AutoSize = true table, there should be no free space. In this case, the TLP reverse-engineers what the size of the column should be based on the infinite preferred size of the contents. E.g. if a control is 50% and it says it wants to be 100px, the other 50% column should be 100px big.

In summary:
Use label.MaximumSize if:
If you have no controls beneath your label AND your label width will remain fixed.
Use label in an anchored, autosized FlowLayoutPanel if:
If you have no controls beneath your label AND your label width will grow as a function of the dialog width.
Use label in a TableLayoutPanel if:
You have controls beneath your label that need to be moved as a function of label text length. You will have to play with the right balance of ColumnStyles and whether or not it is necessary to actually AutoSize the TableLayoutPanel itself.
As a last resort:
If you still cant figure it out, set label.AutoSize = false, and set the label.Size = label.GetPreferredSize( … ) with custom text wrapping constraints.
Updates:
Labels set to FlatStyle.System never word wrap when AutoSize is set to true. Some folks use FlatStyle.System to scoot the text over to line up with the edge of the panel. You can change your Label.Margin.Left = 0 so it will line up with other controls with a Margin.Left = 3.
If you want Wrapping RadioButtons and CheckBoxes read here.
See the designer generated code for the samples!

C# WinForm 中Label自动换行 解决方法的更多相关文章

  1. Sublime Text 2/3中Autoprefixer失效解决方法

    ###Sublime Text 2/3中Autoprefixer失效解决方法: 相信每个前端er都会使用Subl这款工具吧,因为它有上千款开源的插件,而且功能各异,这里给大家带来的是标题中Autopr ...

  2. CAS SSO:汇集配置过程中的错误解决方法

    本教程为gevin.me原创文章,转载请注明: CAS SSO:配置过程中的错误解决方法 | Gevin’s Blog 本文将收集在配置CAS SSO遇到的所有错误,希望对大家有帮助,也方便下次搭建的 ...

  3. 关于获取URL中传值的解决方法--升级版

    这次页面之间的传值是升级版本,为什么是升级版本呢,因为这次页面的传值不一样了.大家可以看一下我原来的文章<关于获取URL中传值的解决方法> 其实上次就已经比较清楚的介绍了页面之间的传值,但 ...

  4. Spring自动扫描无法扫描jar包中bean的解决方法(转)

    转载自:http://www.jb51.net/article/116357.htm 在日常开发中往往会对公共的模块打包发布,然后调用公共包的内容.然而,最近对公司的公共模块进行整理发布后.sprin ...

  5. error LNK2005: “找到一个或多个多重定义的符号” 已经在 xxxx.obj 中定义 的解决方法

    1 问题还原 这里我有三个源文件:Base.hpp, Base.cpp 和 main.cpp 在Base.hpp里面定义一个基类,注意,基类只包含构造函数和析构函数的声明,函数在Base.cpp里实现 ...

  6. 另一种在WINFORM中使用XNA的方法

    之前在写化学分子模型制作程序的时候,使用一种方法,将WINFORM控件嵌入到XNA窗体中,从而实现了即使用WINFORM窗体控件又使用XNA.最近在写另一个物理运动学课件制作程序,同样使用XNA,但从 ...

  7. 【转】Winform程序未捕获异常解决方法 EventType clr20r3 P1

    from:http://blog.csdn.net/chichaodechao/article/details/8294922 在开发winform程序时,用到多线程,在服务器部署后运行,老是自动关才 ...

  8. C# Label换行解决方法

    一.label太短,无法完成显示所要显示信息长度,要换行,解决方法如下: (1) string aa =(长串) ; string cc= aa.Substring(0,10);//取前10个字符 s ...

  9. SpringMVC中404错误解决方法总结

    在新手配置Spring MVC的时候,感觉都弄好了之后,运行起来却显示404错误. 网上对出现404的问题不同情况,都有了解决方法,前几天我也遇到了这个问题,顺便把这些问题总结一下. 解决问题最重要的 ...

随机推荐

  1. jmeter之跨线程组共享cookies

    jmeter在一个线程组里,可以把登录接口放在上面,再添加一个cookies管理器元件,这样下一个接口就可以携带登录信息.在不同线程组里其实也可以共享cookies 目录 1.方法 2.应用 1.方法 ...

  2. C 语言跟 C++ 的差异比较

    C++ 完整的 CHM 版离线手册,可以 从这里下载. C++头文件不必是 .h 结尾 C语言中的标准库头文件,例如 math.h 和 stdio.h,在C++中被命名为 cmath 和 cstdio ...

  3. 流程控制: if分支 while循环 for循环

    流程控制 Python程序执行,一定按照某种规律在执行 1.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构 2.遇到需要条件判断选择不同执行路线的执行方式:分支结构 3.有些事情 ...

  4. sqluldr2 oracle直接导出数据为文本的小工具使用

    近期客户有需求,导出某些审计数据,供审计人进行核查,只能导出成文本或excel格式的进行查看,这里我们使用sqluldr2工具进行相关数据的导出. oracle导出数据为文本格式比较麻烦,sqluld ...

  5. C语言 malloc()、memcpy()、free()等

    1.malloc()函数:  void *malloc(unsigned int num_bytes); 头文件:#include <malloc.h> 或 #include <al ...

  6. MongoDB 基本操作(增改删)

    1.插入数据 和关系型数据库一样,增加数据记录可以使用insert语句,这是很简单的. 当插入数据时,如果此集合不存在,则MongoDB系统会自动创建一个集合,即不需要刻意预先创建集合 每次插入数据时 ...

  7. HDFS网络拓扑概念及机架感知(副本节点选择)

    网络拓扑概念 在本地网络中,两个节点被称为“彼此近邻”是什么意思?在海量数据处理中,其主要限制因素是节点之间数据的传输速率——带宽很稀缺.这里将两个节点间的带宽作为距离的衡量标准. 节点距离:两个节点 ...

  8. sql中的sp_helptext、sp_help 、sp_depends

    sp_help:用于显示参数清单和其数据类型. sp_depends:用于显示存储过程依据的对象或者依据存储过程的对象. sp_helptext:用于显示存储过程的定义文本

  9. wamp 环境的配置

    安装完wamp之后,基本不用配置什么环境,只要将mysql添加到你的环境变量中去即可.

  10. HDU 6468 /// DFS

    题目大意: 把 1~15 的数字典序排序后为 1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9 此时给定 n k, 求1~n的数组字典序排序后 第k个 ...