How can I scroll to a specific line number of a RichTextBox control using C #?

How can I scroll to a specified line number of a RichTextBox control using C #? This is a version of WinForms.

+4
source share
4 answers

You can try something like this.

void ScrollToLine(int lineNumber) { if (lineNumber > richTextBox1.Lines.Count()) return; richTextBox1.SelectionStart = richTextBox1.Find(richTextBox1.Lines[lineNumber]); richTextBox1.ScrollToCaret(); } 

This will not work if you have many repetitions in a RichTextBox. I hope this comes in handy.

+8
source

With this code, the cursor jumps to the first column in the desired row.

It works great anyway.

 void GotoLine(int wantedLine_zero_based) // int wantedLine_zero_based = wanted line number; 1st line = 0 { int index = this.RichTextbox.GetFirstCharIndexFromLine(wantedLine_zero_based); this.RichTextbox.Select(index, 0); } 
+1
source

I'm not sure if it has a method for this, but what about counting the lines in Text , and then set the carriage (via SelectionStart and SelectionLength ) and ScrollToCaret() ?

0
source

Would it help to split the text in this situation? For instance:

string[] lines = myRichTextBox.Text.Split('\n');
int linesCount = lines.Length;

This will tell you the number of rows.

-1
source

All Articles