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);
}
本文介绍了如何使用C#在RichTextBox控件中获取特定行的行号,包括第一行、最后一行及鼠标双击位置所在的行号。通过具体代码示例展示了如何利用GetCharIndexFromPosition和GetLineFromCharIndex方法实现这一功能。
1879

被折叠的 条评论
为什么被折叠?



