如何在RichTextBox中通过编程将光标定位到特定的行和字符?

内容纲要

RichTextBox控件有一个Lines属性,该属性是数组类型,它的每一项对应着RichTextBox控件中的一行。每一项都有对应行的Length属性。那么我们就可以像这样来设置光标:

private void GoToLineAndColumn(RichTextBox rtb, int line, int column)

{

rtb.Focus();

if (line < 0 || column < 0) { rtb.SelectionStart = 0; } else { if (line > rtb.Lines.Length - 1)

{

rtb.SelectionStart = rtb.GetFirstCharIndexFromLine(rtb.Lines.Length - 1)

+ rtb.Lines[rtb.Lines.Length - 1].Length;

}

else

{

int firstCharIndexOfline = rtb.GetFirstCharIndexFromLine(line);

int textlengthOfline;

textlengthOfline = rtb.Lines[line].Length;

if (column > textlengthOfline)

column = textlengthOfline;

rtb.SelectionStart = firstCharIndexOfline + column;

}

}

}

圈主 管理员

热门评论
:
该帖子评论已关闭
图片审查中...
编辑答案: 我的回答: 最多上传一张图片和一个附件
x
x
个人中心
今日签到
有新私信 私信列表
搜索