简体   繁体   English

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

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

I have almost designed a notepad using c#. 我几乎已经使用c#设计了一个记事本。 But only problem I'm facing now is in my statusstrip. 但是,我现在面临的唯一问题是我的身份证明。

My need- I want to display character count per each line. 我的需要-我想显示每行的字符数。

When user press enter key it should come to new line and now character count should start from 1. 当用户按下回车键时,它应该进入新行,现在字符数应该从1开始。

Technically - Col=1, Ln=1; 从技术上讲-Col = 1,Ln = 1; //(initially)Col=no of character per line Ln=line count When user press enter key- Ln=2 and goes on and Col=No of characters we have typed in that specific line I've tried these lines of code - //(最初)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;
        }
    }

Supposing you are using Windows Forms, you can use the following solution (but you have to subscribe to the SelectionChanged event instead of the KeyPress event of the rich text box control): 假设您使用的是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