473,480 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RichTextbox scroll to a line

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
Nov 17 '05 #1
6 29133
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.
"Rachel Suddeth" wrote:
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

Nov 17 '05 #2
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote:
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.
Nov 17 '05 #3
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

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
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.
"Rachel Suddeth" wrote:
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

Nov 17 '05 #4
Rachel Suddeth <ra****@bldhound.com> wrote:
Why is this so hard?


I've never tried using the following, but it looks promising:
<http://tinyurl.com/btoza>.
Nov 17 '05 #5
Thanks... it's worth a try.

"Cool Guy" <co*****@abc.xyz> wrote in message
news:1g***************@cool.guy.abc.xyz...
Rachel Suddeth <ra****@bldhound.com> wrote:
Why is this so hard?


I've never tried using the following, but it looks promising:
<http://tinyurl.com/btoza>.

Nov 17 '05 #6
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

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2684
by: JRB | last post by:
I have a thread adding lines of text to a richtextbox on my windows form about every 1 second. I want the last line of text to always be visible, which it is until the box gets filled up. The last...
3
9375
by: Rachel Suddeth | last post by:
I want to react to scrolls in a RichTextBox (I want to let the user know when he has changed to a new printable page.) I have handled the VScroll() event, but that only responds to scrollbar...
2
17258
by: JonnyT | last post by:
I searched high and low for an answer on how to auto scroll a richtextbox and now I finally have it. Since it took me a while to get a good efficient way of doing it that didn't require focus to...
7
2025
by: Richard | last post by:
When you give focus to a RichTextBox, then it goes to the textCursor, but i don't want to give the richtextbox focus, cause there is being written a lot in it, and I have to do something else at...
0
4129
by: John Smith | last post by:
I have text files and I get binary offsets of text portion of these files from VB6 program with RichTextBox. I use VB.Net 2003 and need to select and highlight the text between given offsets....
1
2238
by: Bruce F | last post by:
From a newbie... I want to load text into a RichTextBox one line at a time so that I can change the font color of each line depending on conditions. 1. How to load line by line? 2. How to...
0
2039
by: Brian Keating | last post by:
Hi there, Does anyone know if i can determin if the scroll position is at the bottom of my textbox? I can get the scroll position fine, but how do i know if it's actually at the bottom? ...
2
1107
by: neoupadhyay | last post by:
Hi Mate, Well i have a datagrid and i use scroll bar on it via "Div" the code is below, its working fine, but i wannt only single scroll line which is verticale...thts i scroll up and down not to...
2
9722
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, I have a somewhat long calculation report printed out in a RichTextBox. To find or monitor a particular value, users scroll down to the location of the data in the RichTextBox. However, when...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7044
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6929
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.