C# 向标签添加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2365967/
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
Adding new line to label
提问by xrx215
I get the data from the database and display that data on the label. I have to apply new line on the text of the label for every 35 charecters. Because after 35 charecters the text overflows.
我从数据库中获取数据并在标签上显示该数据。我必须为每 35 个字符在标签文本上应用新行。因为在 35 个字符后文本溢出。
Can you please let me know how to do this.
你能告诉我如何做到这一点。
采纳答案by Oded
Just change the style of the label so it will wrap the text:
只需更改标签的样式,以便它包裹文本:
style=" width:50px; overflow-y:auto;overflow-x:auto; word-break:break-all;"
Better yet, put this in your CSS file rather than directly on the control.
更好的是,把它放在你的 CSS 文件中,而不是直接放在控件上。
Another option, if you don't like this, is to use a textbox and style it as a label, as described in thispost.
另一种选择,如果你不喜欢这个,就是用一个文本框和风格它作为一个标签,如在此职位。
回答by Asad
use Environment.NewLine
回答by Andrey
there is no way to do this, you would better create more labels. you could add <br/>
, but asp.net will screen it. consider putting single label inside div with fixed width, the text should go to new line automatically.
没有办法做到这一点,你最好创建更多的标签。你可以添加<br/>
,但asp.net会屏蔽它。考虑将单个标签放在具有固定宽度的 div 中,文本应自动转到新行。
回答by CodeMonkey1313
//assuming l is a label
for (int i = 0; i < l.Text.Length; i++)
{
if (i % 35 == 0)
l.Text.Insert(i, Environment.NewLine) // or use "<br />" for html break since browsers ignore whitespace (except IE 6 in some cases)
}
回答by JDMX
quick psuedo code
快速伪代码
string s = 'value from db';
string s2 = "";
int len = s.Length;
int i = 0;
while ( i + 35 > len ) {
s2 += s.Substring( i, 35 ) + "\r\n";
i+=35;
}
s2 += s.Substring( i, len - i );
label.Text = s2;
if \r\n does not work, replace it with < br >
如果 \r\n 不起作用,则将其替换为 <br>
回答by emre
On Properties window you can change the label's Width and Height. You should set the label size based on the size of window and size of label type must pixel.
在属性窗口中,您可以更改标签的宽度和高度。您应该根据窗口大小和标签类型的大小来设置标签大小必须是像素。