繁体   English   中英

计算C#中的statusstrip中每行(富文本框)的字符数

[英]counting number of characters per each line(rich textbox) in statusstrip in c#

我几乎已经使用c#设计了一个记事本。 但是,我现在面临的唯一问题是我的身份证明。

我的需要-我想显示每行的字符数。

当用户按下回车键时,它应该进入新行,现在字符数应该从1开始。

从技术上讲-Col = 1,Ln = 1; //(最初)Col =每行字符数Ln =行数当用户按下Enter键-Ln = 2并继续并且Col =我们在该特定行中键入的字符数我尝试了这些代码行-

 private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        int count = Convert.ToInt32(e.KeyChar);
        if (Convert.ToInt32(e.KeyChar) != 13)
        {
            Col = richTextBox1.Text.Length;
            toolStripStatusLabel1.Text = "Col:" + Col.ToString() + "," + "Ln:" + Ln;
        }
        if (Convert.ToInt32(e.KeyChar) == 13)
        {
            //richTextBox1.Clear(); 
            Ln = Ln + 1;
            toolStripStatusLabel1.Text = "Col:" + Col.ToString() + "Ln:" + Ln;
        }
    }

假设您使用的是Windows Forms,则可以使用以下解决方案(但是您必须订阅SelectionChanged事件而不是富文本框控件的KeyPress事件):

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    int currentIndex = richTextBox1.SelectionStart;

    // Get the line number of the cursor.
    Ln = richTextBox1.GetLineFromCharIndex(currentIndex);

    // Get the index of the first char in the specific line.
    int firstLineCharIndex = richTextBox1.GetFirstCharIndexFromLine(Ln);

    // Get the column number of the cursor.
    Col = currentIndex - firstLineCharIndex;

    // The found indices are 0 based, so add +1 to get the desired number.
    toolStripStatusLabel1.Text = "Col:" + (Col + 1) + "   Ln:" + (Ln + 1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM