C# – How to get the current line in a RichTextBox control

cnetrichtextboxwinforms

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.

Best Answer

This worked for me:

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