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.
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.
来源:https://stackoverflow.com/questions/2693984/how-to-get-the-current-line-in-a-richtextbox-control