RichTextbox scroll to a line

R

Rachel Suddeth

I have the index of a line in the Lines array of a RichTextBox. I would like
to have it scroll so that line displays at the top. Is there no way to do
this?

The only way I can see to make it scroll is with ScrollToCaret(). But I
don't know how to set the "caret" to a particular line.

Any suggestions?

-Rachel
 
G

Guest

Hi Rachel,
one way to do it would be the following:

//have to set focus first on the rich text box for scrolling to work
this.richTextBox1.Focus();

//move the caret to the correct place <IMPO>This is a character position not
line position</IMPO>
this.richTextBox1.SelectionStart = 50;

this.richTextBox1.ScrollToCaret();


I don't think there is a method for setting to caret to an individual line,
so you will have to calculate the position of the caret by knowing how many
characters there are in each line (plus the \r\n)

Hope that helps
Mark.
 
C

Cool Guy

Mark R. Dawson said:
I don't think there is a method for setting to caret to an individual line,
so you will have to calculate the position of the caret by knowing how many
characters there are in each line (plus the \r\n)

Small point: the line delimiter that RichTextBox uses is simply '\n' in my
testing.
 
R

Rachel Suddeth

Maybe I should just track the current character position intead. Now that I
think of it, that's just as easy to do -- it's just that the line index was
what I needed for other purposes, so it's what I was tracking. But it's easy
to do GetLineFromCharIndex(), and not so easy the other way 'round. I don't
feel a whole lot of confidence that I can always count on exactly one
character between each line... (which is what I'm seeing in my testing...
just the '\n' -- which is translated to a "/r/n" in the .Rtf property.

In order for ScrollToCaret to do what I really want, I'll also need to know
whether my initial position is above or below the place I want to scroll to,
and if it's above (so I'm scrolling down)... oh, thats the problem, though.
I can figure out how many lines are showing in the control, but to get the
"caret" down that many lines (so that my desired line will end up as the
first line, not the last), I will still need to be able to find a character
position n lines down ...

Why is this so hard? Some part of the control has to know which line its
characters are on to draw them correctly... why don't I have access to that
info?

I could try GetCharIndexFromPosition() where the Position argument has a Y
close to RichTextBox.ClientSize.Height * 2, and then ScrollToCaret a second
time... maybe that would work, but it seems unnecessarily complicated, and I
don't know how it would look.

-Rachel
 
R

Rachel Suddeth

Looks like this will work. At least I've tested with this (in Leave event of
TextBox1):
// const int EM_LINESCROLL = 0x00B6;
SendMessage( RichTextbox1.Handle, 0x00b6, 0, int.Parse( TextBox1.Text) );

and it does indeed scroll down the number of lines I type into TextBox1.Text
 

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