How to get the current line in a RichTextBox control?

read richtextbox line by line c#
richtextbox c#

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.

How to get the current line inside a richTextBox?, RichTextBox has Lines property as well as textBox control. So you want to get the text of line4, you must use: RichTextBox1.Lines(3).ToString. Occurs when the current selection of text in the RichTextBox control has changed or the insertion point has moved. You can use the SelChange event to check the various properties that give information about the current selection (such as SelBold ) so you can update buttons in a toolbar, for example.

This worked for me:

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

Get and Set current line in a RichTextBox - MSDN, Retrieves the line number from the specified character position within the text of in a form containing a RichTextBox control named RichTextBox1 , a button named Text = ""; // Get the word to search from from TextBox2. string searchWord� 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.

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

RichTextBox.GetLineFromCharIndex(Int32) Method (System , 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� The Text property does not return any information about the formatting applied to the contents of the RichTextBox. To get the rich text formatting (RTF) codes, use the Rtf property. The amount of text that can be entered in the RichTextBox control is limited only by available system memory. Applies to See also. Lines; Multiline

My answer is even more awesome than Vincent's because after getting the right line number I noticed it was flickering, horribly. So I added an in memory rich text box to do the work there.

private int GetCharacterIndexOfSelection()
{
    var wordWrappedIndex = this.SelectionStart;

    RichTextBox scratch = new RichTextBox();
    scratch.Lines = this.Lines;
    scratch.SelectionStart = wordWrappedIndex;
    scratch.SelectionLength = 1;
    scratch.WordWrap = false;
    return scratch.SelectionStart;
}

private int GetLineNumberOfSelection()
{
    var selectionStartIndex = GetCharacterIndexOfSelection();

    RichTextBox scratch = new RichTextBox();
    scratch.Lines = this.Lines;
    scratch.SelectionStart = selectionStartIndex;
    scratch.SelectionLength = 1;
    scratch.WordWrap = false;
    return scratch.GetLineFromCharIndex(selectionStartIndex);
}

How to get the current line in a RichTextBox control? - c# - csharp, Do anyone know how to get the current line number from a RichTextBox when wrapping is enabled? The ststement I Dim control1 As Control You can get a paragraph by calling Paragraph para=CaretPosition.Paragraph; If you want to remove this paragraph, use Document.Blocks.Remove(para). if you want to remove the line in which the Caret is, you should find two TextPointers: The one at the begining of the line and the one at the end. Call these two tp1 and tp2. You can remove the line by

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.

How to get the actual current line number of a richtextbox, the line number is simple. The RichTextBox control has a property called SelectionStart. So getting the current line number is as simple as: int line, index ;. Basically I'm checking a point just inside the upper-left corner of the richtextbox and a point just inside the lower left corner of the textbox. You may have to adjust the point coordinates depending on how your richtextbox is displayed. Then I get the character index that is closest to each of those two points, and retrieve the line that it

[PDF] C# How-to 9: Get the position of the caret in a text box, These are the top rated real world C# (CSharp) examples of System. < summary> /// Gets the current line from a RichTextBox control. BackColor; // Get number of actual lines the text box needs // given the current width int lines = (1 + tb. If GotoLine Then 'If Go To line is checked 'Get pos of start of the line lngStart = SendMessage(.hWnd, EM_LINEINDEX, txtGo.Text - 1, 0&) If lngStart = -1 Then 'Invalid line number MsgE "Invalid line number!", "ElitePad - Go To", 0, True Exit Sub End If .SelStart = lngStart 'Go To line ElseIf GotoStart Then 'Go To start of the document .SelStart = 0

RichTextBox.GetLineFromCharIndex, System.Windows.Forms C# , Displaying line numbers on a page has been an often requested feature and this sample shows how to calculate the required positions. so that the line number is displayed next to the current line. int left = line.TextBounds. RichTextBox Control Overview Introduces the general concepts of the RichTextBox control, which allows users to enter, display, and manipulate text with formatting options. How to: Determine When Formatting Attributes Change in the Windows Forms RichTextBox Control Explains how to keep track of changes in font and paragraph formatting in the

Displaying Line Numbers in Windows Forms and , The method begins by locating the start and end offset for the current line being edited. Next I extract this line of text from the RichTextBox so it�