How to get the current line in a RichTextBox control?

a 夏天 提交于 2019-12-05 19:31:28

That's what RichTextBox.GetLineFromCharIndex() does. Pass the SelectionStart property value.

This worked for me:

this.WordWrap = false;
int cursorPosition = this.SelectionStart;
int lineIndex = this.GetLineFromCharIndex(cursorPosition);
string lineText = this.Lines[lineIndex];
this.WordWrap = true;

One way is to send it the EM_LINEFROMCHAR Message. I'm sure there are other ways.

user718424

If you want to get current Line Number from the editor Control where you are debugging now then

int lineNumber = (new StackTrace()).GetFrame(1).GetFileLineNumber();

I think it is helpful for your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!