How to get the current line in a RichTextBox control?

≡放荡痞女 提交于 2019-12-22 10:22:59

问题


Say I clicked somewhere inside a RichTextBox control. How can I get the current line the caret is currently on?

Btw this is to retrieve the whole text string of that line.


回答1:


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




回答2:


This worked for me:

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



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/2693984/how-to-get-the-current-line-in-a-richtextbox-control

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