C# Richtextbox 行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17520642/
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
Richtextbox lines count
提问by Los799
I'm developing a text editor in C#, and I'm trying to make a line count.
我正在用 C# 开发一个文本编辑器,我正在尝试计算行数。
private void updateNumberLabel()
{
Point pos = new Point(0, 0);
int firstIndex = Document.GetCharIndexFromPosition(pos);
int firstLine = Document.GetLineFromCharIndex(firstIndex);
pos.X = ClientRectangle.Width;
pos.Y = ClientRectangle.Height;
int lastIndex = Document.GetCharIndexFromPosition(pos);
int lastLine = Document.GetLineFromCharIndex(lastIndex);
int actualLine = Document.GetLineFromCharIndex(actualPos);
pos = Document.GetPositionFromCharIndex(lastIndex);
if (lastLine != actualLine)
{
numberLabel.Text = "";
for (int i = firstLine; i <= lastLine + 1; i++)
{
numberLabel.Text += i + 1 + "\n";
}
}
}
It works fine and adds the number of lines while you write them, but if you delete one, it will only update if you delete or add one more line.
它工作正常并在您编写行数时添加行数,但如果您删除一行,则只有在您删除或再添加一行时才会更新。
I want make it instantaneous. If you delete one, the count shall be decreased instantaneously.
我想让它瞬间完成。如果删除一个,计数将立即减少。
回答by DerApe
Maybe this is too easy, but what about that:
也许这太简单了,但那又如何:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}
Make sure you assign it to the TextChangedevent.
确保将其分配给TextChanged事件。
If this is not what you need, please add some more information what you are trying to achieve.
如果这不是您所需要的,请添加更多您想要实现的信息。
回答by Pharap
Instead of trying to battle with the default rich text box, why don't you try making your own control so you have full control of the text formatting?
与其尝试使用默认的富文本框,不如尝试制作自己的控件,以便完全控制文本格式?
After all, if you're developing your own text editor, it would make sense to have text stored and managed in a way that makes sense to you, the developer, instead of trying to fight with a format designed for a slightly different purpose.
毕竟,如果您正在开发自己的文本编辑器,那么以一种对您(开发人员)有意义的方式存储和管理文本是有意义的,而不是试图与为稍微不同目的而设计的格式作斗争。
回答by Richtenzorg
Los799
洛斯799
Sorry for answering a "bit" late, I saw this question just now. But if the problem still stands, here's a simple way to count lines, just use a foreach loop:
抱歉回答“有点”晚了,我刚刚看到这个问题。但是如果问题仍然存在,这里有一个简单的方法来计算行数,只需使用 foreach 循环:
int CountOfLines = 1;//1 because min 1 line is always in a text of a control, that has a Text property
foreach (char c in YourText)
{
if (c == '\r' | c == '\n')//these are all equal the ENTER key
{
CountOfLines++;
}
}
You can also use foreach to count characters as well, but with foreach you can choose characters, that you don't want to be counted in the count of characters. For example:
您也可以使用 foreach 来计算字符数,但是使用 foreach 您可以选择字符,您不希望被计算在字符数中。例如:
int CountOfCharacters = 0;//0 because by default there are no characters in a textbox or label.
foreach (char c in YourText)
{
if (c != '\t' & c != '\n' & c != '\r')//in this example I want to count only the characters that are not an ENTER or a TAB.
{
CountOfCharacters++;
}
}
Hope this helps you and everybody else, who's reading this, even if it's a bit late answer. :)
希望这对你和其他正在阅读本文的人有所帮助,即使答案有点晚。:)
回答by Sirentec
I'm really late, but Lines is an array of string. Just get the length.
我真的迟到了,但 Lines 是一个字符串数组。只要得到长度。
richTexBox.Lines.Length.ToString();
回答by SHO
I have found one open source and applied it to this problem.
我找到了一个开源并将其应用于这个问题。
I have confirmed that it works well and have implemented it.
我已经确认它运行良好并已实施。
Simple Code( The link has a demo source.):
简单代码(链接有演示源。):
this.rtb.CursorPositionChanged +=
new System.EventHandler(this.rtb_CursorPositionChanged);
this.rtb.SelectionChanged +=
new System.EventHandler(this.rtb_SelectionChanged);
.
.
.
private void rtb_CursorPositionChanged(object sender, System.EventArgs e)
{
int line = rtb.CurrentLine;
int col = rtb.CurrentColumn;
int pos = rtb.CurrentPosition;
statusBar.Text = "Line " + line + ", Col " + col +
", Position " + pos;
}
private void rtb_SelectionChanged(object sender, System.EventArgs e)
{
int start = rtb.SelectionStart;
int end = rtb.SelectionEnd;
int length = rtb.SelectionLength;
statusBar.Text = "Start " + start + ", End " + end +
", Length " + length;
}
namespace Nik.UserControls
{
public class RicherTextBox2 : System.Windows.Forms.RichTextBox
{
public event EventHandler CursorPositionChanged;
protected virtual void OnCursorPositionChanged( EventArgs e )
{
if ( CursorPositionChanged != null )
CursorPositionChanged( this, e );
}
protected override void OnSelectionChanged( EventArgs e )
{
if ( SelectionLength == 0 )
OnCursorPositionChanged( e );
else
base.OnSelectionChanged( e );
}
public int CurrentColumn
{
get { return CursorPosition.Column( this, SelectionStart ); }
}
public int CurrentLine
{
get { return CursorPosition.Line( this, SelectionStart ); }
}
public int CurrentPosition
{
get { return this.SelectionStart; }
}
public int SelectionEnd
{
get { return SelectionStart + SelectionLength; }
}
}
internal class CursorPosition
{
[System.Runtime.InteropServices.DllImport("user32")]
public static extern int GetCaretPos(ref Point lpPoint);
private static int GetCorrection(RichTextBox e, int index)
{
Point pt1 = Point.Empty;
GetCaretPos(ref pt1);
Point pt2 = e.GetPositionFromCharIndex(index);
if ( pt1 != pt2 )
return 1;
else
return 0;
}
public static int Line( RichTextBox e, int index )
{
int correction = GetCorrection( e, index );
return e.GetLineFromCharIndex( index ) - correction + 1;
}
public static int Column( RichTextBox e, int index1 )
{
int correction = GetCorrection( e, index1 );
Point p = e.GetPositionFromCharIndex( index1 - correction );
if ( p.X == 1 )
return 1;
p.X = 0;
int index2 = e.GetCharIndexFromPosition( p );
int col = index1 - index2 + 1;
return col;
}
}
}

