在c#中将一行拆分为多行 [英] Split one line into many lines in c#

查看:163
本文介绍了在c#中将一行拆分为多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

假设我有一条线,即

Thisssssssssssssssssssssssssssssssssss Loooooooooong Teeeeeeeeeeeeeeeeeeeeeeext



我必须将这一行放在许多行中。到面板的宽度。那是我写一些随机文本给label.text(放在面板中)。然后,面板剩余文本的大小将转移到下一行,如果剩余的文本仍然大于面板大小,文本将再次中断,剩余的文本将转到第3行



喜欢: -

Thissssssssssssssssssssssssssssssss

Loooooooooong Teeeeeeeeeee

eeeeeeeeeeext



这是Windows窗体中的所有内容。

我们非常感谢代码和指导。

Hello,
Let suppose I've a line i.e
"Thisssssssssssssssssss Isssssssss Loooooooooong Teeeeeeeeeeeeeeeeeeeeeeext"

I have to place this single line in many lines acc. to the width of the panel. That is when I write some random text given to a label.text (which is placed in panel). then after the size of panel remaining text will transfer to the next line and if remaining text is still larger than panel size than the text will again break and that remaining text will go to the 3rd line

Like:-
Thisssssssssssssssssss Isssssssss
Loooooooooong Teeeeeeeeeee
eeeeeeeeeeext

This is all in windows form.
Code and guidance is highly appreciated.

推荐答案

这是您的解决方案:当文本大于c#中面板的大小时,如何增加面板的大小在WinForms中 [ ^ ]
Here is your solution: how to increase the size of panel when text is greater than the size of the panel in c# in WinForms[^]


这不是一项简单的任务,甚至不是轻微的任务。



主要问题是文本框的默认字体是比例字体:charcaters的宽度不同。例如,i比m窄 - 这使得你能够比你想象的更难以适应多少个字符,因为一个单词的宽度根据拼写的字符而变化。 br />


你可以这样做:看看 System.Drawing.Graphics.MeasureString [ ^ ]然后解决它,但最简单的解决方案是在面板上填充一个设置为Multiline的停靠文本框,并将WordWrap设置为true并让它全部排序! :笑:
This is not a simple task, not even slightly.

The major problem is that the default font for text boxes is a proportional font: the charcaters are not all the same width. "i" is narrower than "m" for example - this makes working out how many characters you can fit to a "line" much harder than you might think because the width of a word varies depending on what characters it is spelled with.

You can do it: look at System.Drawing.Graphics.MeasureString[^] and then work it out, but the simplest solution is to fill the panel with a docked textbox set to "Multiline" and WordWrap both set to "true" and let it sort it all out! :laugh:


你可以通过包装



为了使它在特定宽度上自动换行,你可以设置MaximumSize属性。



Label1.MaximimSize = new Size(100,0);

Label1.AutoSize = true;





并使您的容器面板Auto Scroll属性为true。
You can do this by wrapping

In order to make it word wrap at a particular width, you can set the MaximumSize property.

Label1.MaximimSize = new Size(100, 0);
Label1.AutoSize = true;


And Make your container panel Auto Scroll property to true.


这篇关于在c#中将一行拆分为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆