Richtextbox行计数

时间:2013-07-08 06:52:59

标签: c# winforms count richtextbox lines

我正在用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";
            }
        }
    }

它工作正常并在您编写行时添加行数,但如果删除行,则只有在删除或添加一行时才会更新。

我想让它瞬间完成。如果删除一个,则应立即减少计数。

6 个答案:

答案 0 :(得分:4)

也许这太简单了,但是那个:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
  var lineCount = richTextBox.Lines.Count();
  numberLabel.Text = lineCount.ToString();
}

确保将其分配给TextChanged事件。

如果这不是您所需要的,请添加一些您想要实现的信息。

答案 1 :(得分:0)

Los799

对于迟到的回答,很抱歉,我刚才看到了这个问题。 但如果问题仍然存在,这里有一个计算行的简单方法,只需使用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++;
                }
            }

您也可以使用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++;
                }
            }

希望这可以帮助你和其他所有人,即使这是一个迟到的答案。 :)

答案 2 :(得分:0)

我真的很晚了,但是Lines是一个字符串数组。得到长度。

richTexBox.Lines.Length.ToString();

答案 3 :(得分:0)

我找到了一个开源并将其应用于此问题。

我已经确认它可以正常运行并实现。

RichTextBox Colums and Row

简单代码( 该链接有一个演示源。):

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;
         }
    }
}

答案 4 :(得分:-1)

为什么不尝试自己控制,以便完全控制文本格式,而不是尝试使用默认的富文本框?

毕竟,如果您正在开发自己的文本编辑器,那么以对您(开发人员)有意义的方式存储和管理文本是有意义的,而不是尝试使用专为稍微设计的格式进行操作不同的目的。

答案 5 :(得分:-1)

针对 WPF 应用程序此问题的较新解决方案 DOK1 是一个 WPF 流文档 TX1 是一个 TextBox 控件 TX1.Text = DOK1.Blocks.Count.ToString();