(Rich)TextBox, scrolling, HideSelection

A

a_n_d_y_bell

Hi-
In my application I have a RichTextBox, and I want to scroll it so that
the character at a given index appears on the top line of the box. Up
till now, I've used this routine, which works fine:

==
rtb.Focus();
rtb.SelectionStart=rtb.Text.Length-1;
rtb.SelectionLength=0;
rtb.SelectionStart=rtb.Text.IndexOf("MY_TEXT");
rtb.SelectionLength=0;
==

However, recently I've been adding a search feature, and as a result
I've needed to set the RTB's HideSelection property to false. An
unfortunate side-effect of this is that for some reason the above code
no longer works. I've experimented with ScrollToCaret combinations, but
the problem is that if MY_TEXT is visible on-screen but half-way down
the textbox, then calling ScrollToCaret will never move the box.

Does anyone have a solution?

Cheers,
Andy
 
C

C# Learner

In my application I have a RichTextBox, and I want to scroll it so that
the character at a given index appears on the top line of the box. Up
till now, I've used this routine, which works fine:

[...]

However, recently I've been adding a search feature, and as a result
I've needed to set the RTB's HideSelection property to false. An
unfortunate side-effect of this is that for some reason the above code
no longer works.

I've experimented with ScrollToCaret combinations, but
the problem is that if MY_TEXT is visible on-screen but half-way down
the textbox, then calling ScrollToCaret will never move the box.

I'm unable to reproduce your problem (whether HideSelection is true or
false). Here's the relevant code from my test project:

rtb.Focus();
rtb.SelectionStart=rtb.Text.Length-1;
rtb.SelectionLength=0;
rtb.SelectionStart=rtb.Text.IndexOf("MY_TEXT");
rtb.SelectionLength=0;
rtb.ScrollToCaret();

The RichTextBox is pre-filled with some 100 lines of text, with "MY_TEXT"
occuring once halfway through. When I run the above code, the RichTextBox
scrolls so that the line containing "MY_TEXT" is the top visible line.

Are you sure you're calling RichTextBox.Focus in your real code? This
could cause your problem (but only if HideSelection is true).

If you still can't solve this, create a new project and add the relevant
code to the form's file, and post that file here (either attach it to your
post or upload it to some file server and provide a link to it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top