?
Solved

I need a way to GoTo a specified line number in a RichTextBox display in C#

Posted on 2009-12-16
2
Medium Priority
?
1,897 Views
Last Modified: 2013-12-17
I need a way to GoTo a specified line number in a RichTextBox display in C#.
In other words&
I would like the line number requested to become the 1st line displayed in the visabe (on-screen) RichTextBox.
I seem to be able to set the selection using the following

   int lineno = 20; // example: 20 is a legal line number in the Richtextbox.
   int index = this.scriptContainer.GetFirstCharIndexFromLine(lineno);
   this.scriptContainer.Select(index, 1);
However the RTB control does not seem to automatically jump to display the selection in the visable RichTextBox.
What Am I missing?
I cant seem to find a simple GoTo line number ;-)

Thank you for your kind assistance.
dannyhop
0
Comment
Question by:dannyhop
2 Comments
 
LVL 4

Accepted Solution

by:
utter77 earned 2000 total points
ID: 26064671
Hi. I had the same problem. Solved it like this:

int row = 12; //row to scroll to
int col = 33; // column to scroll to
match.length = selection length.


            int matchPos = 0;
            string[] newLines = richTextBox1.Lines;

            if (newLines.Length >= row)
            {
                //COUNT POSITIONS IN EACH ROW UNTIL MATCHED ROW
                for (int i = 0; i < row - 1; i++)
                {
                    matchPos += newLines[i].Length + 1;
                }
               
                richTextBox1.Select(matchPos + col -1, match.Length); //SETS CARET TO BEGINNING OF MATCHED ROW
                //SCROLL TO MATCH
                richTextBox1.ScrollToCaret();
            }
0
 

Author Closing Comment

by:dannyhop
ID: 31666886
Dang,
All I really needed was to add .ScrollToCaret().
This now works...
   int lineno = 20; // example: 20 is a legal line number in the Richtextbox.
   int index = this.scriptContainer.GetFirstCharIndexFromLine(lineno);
   this.scriptContainer.Select(index, 1);
   this.scriptContainer.ScrollToCaret()

And to think, I have used ScrollToCaret() before.  I must be losing it ;-)
Thanks...  The last line in your example was what I was missing....
Anybody know of a good RichTextBox tutorial?  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question