问题
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