mathinvalidnik
mathinvalidnik

Reputation: 1600

How to scroll the text in a RichTextBox to a particular prhase?

I have a RichTextBox that conatins text and after text loaded I want the scroller to show the particular phrase that i get dinamycally.

Example: I want to scroll to "And thanks again pal!" which is at the end of my text and if I dont know that it is there I have scrool all the way down.

I was wondering if there is a way in .net that thing to happen without scrolling?

Upvotes: 1

Views: 106

Answers (2)

Mikhail Tregubov
Mikhail Tregubov

Reputation: 68

Try this:

        rtb_Log.Text = "This text is not so special.";
        string findstring = "special.";
        rtb_Log.Select(rtb_Log.Find(findstring, RichTextBoxFinds.MatchCase), 0);
        rtb_Log.Focus();

Valid for .net 4.0 winforms control.

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

First you'll need to find the index of that phrase:

var index = richTextBox.Text.IndexOf("my phrase");

and then you'll need to set the SelectionStart to that index:

richTextBox.SelectionStart = index;

so now the text box is in the right position, now you can do this:

richTextBox.ScrollToCaret();

Upvotes: 2

Related Questions