如何检查RichTextBox中是否已启动新行 [英] How to check if a new line has started in a RichTextBox
问题描述
我在使用多个richTextBox的表单时遇到问题。
I'm having an issue with a form that I have made where I use multiple richTextBoxes.
我的问题是,如果有人在字段中输入并到达边缘正如您所期望的那样,它移动到一个新行的richtextbox,然而当我在打印机上打印该表单时,它不会将文本移动到新行,而是继续从页面上继续所有
的方式。
My problem is that if someone types in the fields and reaches the edge of the richtextbox it moves to a new line, as you would expect, however when I then print the form out on a printer it does not move the text to a new line and instead continues all the way off the page.
我已经实现了代码,用于检查字符串数组中是否存在'\ n',如果有,则将下一个字符串移动到新行,但我认为当用户到达文本框的边缘时,它不会将"\ n"添加到字符串数组中,而是将
只是以图形方式将文本移动到新行。
I have implemented code that checks if there is a '\n' in the string array and if there is then move the next string to a new line but I think that when the user reaches the edge of the text box it is not adding the '\n' to the string array and is instead just moving the text to a new line graphically.
这是我的代码,用于检查字符串数组中是否有'\ n'。
Here is my code that checks if there is a '\n' in the string array.
char [] param = {' \ n'};
char[] param = { '\n' };
foreach(字符串s in richTextBox3.Text.Split(param))
              {
                  e.Graphics.DrawString(s,richTextBox3.Font,newB,50,y3);
                  y3 + =(f.Height + 3);
              }
foreach (string s in richTextBox3.Text.Split(param))
{
e.Graphics.DrawString(s, richTextBox3.Font, newB, 50, y3);
y3 += (f.Height + 3);
}
如果用户手动点击回车键移动到richTextBox中的新行,则没有问题。只有当richTextBox自动移动到新行时才会检测到新行。
If the user manually hits the enter key to move to a new line in the richTextBox there isn't a problem. It's only when the richTextBox automatically moves to a new line that when printed it doesn't detect a new line.
推荐答案
试试这个例子:
Try this example:
string text = richTextBox3.Text;
var lines = new List<string>();
int p = 0;
for( int i = 1; ; ++i )
{
int f = richTextBox3.GetFirstCharIndexFromLine( i );
if( f < 0 )
{
lines.Add( text.Substring( p ) );
break;
}
lines.Add( text.Substring( p, f - p ) );
p = f;
}
检查行数组。
您还可以禁用包装并引入滚动条;然后用户将使用< Enter>手动分隔线。
You can also disable the wrapping and introduce scrollbars; then the users will break the lines manually using <Enter>.
这篇关于如何检查RichTextBox中是否已启动新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!