在我的 windows.forms C# 应用程序中,我有一个 WordWrap = true 的多行文本框。将 Text 属性设置为长字符串后,我需要获取通过换行生成的所有行。它与 Lines[] 属性不同,因为我的文本不包含换行符。
我已经找到了使用图形 MeasureString 函数的解决方案,但考虑到文本框控件已经完成了换行,这似乎有点额外的工作 - 为什么我应该再次做同样的工作?
有什么方法可以获取文本框将文本换行的行吗?
谢谢
您可以检查以下解决方案吗?
public Form1()
{
InitializeComponent();
textBox1.Text = "This is my text where I want to check how I can get wrapped content as seperate lines automatically !! This is my text which I want to check how I can get wrapped content as seperate lines automatically !!";
}
private void button1_Click(object sender, EventArgs e)
{
bool continueProcess = true;
int i = 1; //Zero Based So Start from 1
int j = 0;
List<string> lines = new List<string>();
while (continueProcess)
{
var index = textBox1.GetFirstCharIndexFromLine(i);
if (index != -1)
{
lines.Add(textBox1.Text.Substring(j, index - j));
j = index;
i++;
}
else
{
lines.Add(textBox1.Text.Substring(j, textBox1.Text.Length - j));
continueProcess = false;
}
}
foreach(var item in lines)
{
MessageBox.Show(item);
}
}
GetFirstCharIndexFromLine 参考 https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.getfirstcharindexfromline(v=vs.110).aspx
文本框中的行编号从零开始。如果行号
参数大于文本框中的最后一行,
GetFirstCharIndexFromLine 返回 -1。
GetFirstCharIndexFromLine 返回 a 的第一个字符索引
物理线。物理线是显示的线,而不是
指定的线路。显示的行数可以大于
由于自动换行而分配的行数。例如,如果您分配
将两行长行添加到 RichTextBox 控件并设置 Multiline 和 WordWrap
为 true 时,两条长分配线会产生四个物理(或
显示行)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)