C#中RichTextBox获得当前行号的方法

    技术2026-08-17  20

       int charIndex = this.richTextBox1.GetCharIndexFromPosition(new Point(x, y));    int line = this.richTextBox1.GetLineFromCharIndex(charIndex);

    其中x,y为坐标位置

     

    如想获得RichTextBox窗口内第一行的行号(因窗口可滚动,第1行的行号不一定是1)

        可指定x,y=5,5

     

    如想获得RichTextBox窗口内最后一行的行号

        可指定x,y = 5, this.richTextBox1.Height - 25; (25是减去水平滚动条的高度)

     

    如想获得RichTextBox窗口中鼠标双击所在行的行号

            private void richTextBox1_MouseDoubleClick(object sender, MouseEventArgs e)         {             int charIndex = this.richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));             int line = this.richTextBox1.GetLineFromCharIndex(charIndex);         }

     

    最新回复(0)