473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to auto scroll a richtextbox in Visual Basic .Net

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 the
control or the scrolltocaret method I decided that it would be worth
posting to anyone who might have similar concerns. For the record
this works well in an environment where you cannot have focus going to
the richtextbox....such as a chat program or something similar. If
you are able to set focus to the form then use this method

richtextbox.selectionstart = richtextbox.textlength
richtextbox.scrolltocaret()

I think that should do it...can't remember offhand...something like
that though.

Now if you cannot set the focus to the richtextbox then this will work
well for you.
When you want to display text in the richtext box use the
..appendtext("sometext") method. That's what it's there for.
Then you will need the sendmessage API...

Public Declare Function SendMessage Lib "user32.dll" Alias _
"SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

and you will also need a couple constants...

Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINESCROLL = &HB6

Now the way I do it works great for me....

I figure out how many line are in the richtextbox before adding my new
text to it....I do that with the following...

Dim intLines As Integer = SendMessage(richtextbox.Handle,
EM_GETLINECOUNT, 0, 0)

then add your text

richtextbox.appendtext("your text here")

you will know how many lines to scroll down with by running the
EM_GETLINECOUNT after you added your new text like so

Private intLinesToAdd as integer = (SendMessage(richtextbox.Handle,
EM_GETLINECOUNT, 0, 0) - intTotalLines)

Now that you know how many lines have been added...you know how many
lines your scroll bar needs to scroll down.
We use this line to scroll the scroll bar down...

SendMessage(richtextbox.Handle, EM_LINESCROLL, 0, intLinesToAdd)

I've changed it a little from my original code to make it easier for
you to use...I hope it still works how it's suppose to. If not I
think you get the jist of it.
JonnyT
Nov 20 '05 #1
2 17226
Cor
Hi Jonny,

Thank you for sharing this with us.

Cor
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 the
control or the scrolltocaret method I decided that it would be worth
posting to anyone who might have similar concerns. For the record
this works well in an environment where you cannot have focus going to
the richtextbox....such as a chat program or something similar. If
you are able to set focus to the form then use this method

richtextbox.selectionstart = richtextbox.textlength
richtextbox.scrolltocaret()

I think that should do it...can't remember offhand...something like
that though.

Now if you cannot set the focus to the richtextbox then this will work
well for you.
When you want to display text in the richtext box use the
.appendtext("sometext") method. That's what it's there for.
Then you will need the sendmessage API...

Public Declare Function SendMessage Lib "user32.dll" Alias _
"SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

and you will also need a couple constants...

Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINESCROLL = &HB6

Now the way I do it works great for me....

I figure out how many line are in the richtextbox before adding my new
text to it....I do that with the following...

Dim intLines As Integer = SendMessage(richtextbox.Handle,
EM_GETLINECOUNT, 0, 0)

then add your text

richtextbox.appendtext("your text here")

you will know how many lines to scroll down with by running the
EM_GETLINECOUNT after you added your new text like so

Private intLinesToAdd as integer = (SendMessage(richtextbox.Handle,
EM_GETLINECOUNT, 0, 0) - intTotalLines)

Now that you know how many lines have been added...you know how many
lines your scroll bar needs to scroll down.
We use this line to scroll the scroll bar down...

SendMessage(richtextbox.Handle, EM_LINESCROLL, 0, intLinesToAdd)

I've changed it a little from my original code to make it easier for
you to use...I hope it still works how it's suppose to. If not I
think you get the jist of it.
JonnyT

Nov 20 '05 #2
Cor
Hi Jonny,

Thank you for sharing this with us.

Cor
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 the
control or the scrolltocaret method I decided that it would be worth
posting to anyone who might have similar concerns. For the record
this works well in an environment where you cannot have focus going to
the richtextbox....such as a chat program or something similar. If
you are able to set focus to the form then use this method

richtextbox.selectionstart = richtextbox.textlength
richtextbox.scrolltocaret()

I think that should do it...can't remember offhand...something like
that though.

Now if you cannot set the focus to the richtextbox then this will work
well for you.
When you want to display text in the richtext box use the
.appendtext("sometext") method. That's what it's there for.
Then you will need the sendmessage API...

Public Declare Function SendMessage Lib "user32.dll" Alias _
"SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

and you will also need a couple constants...

Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINESCROLL = &HB6

Now the way I do it works great for me....

I figure out how many line are in the richtextbox before adding my new
text to it....I do that with the following...

Dim intLines As Integer = SendMessage(richtextbox.Handle,
EM_GETLINECOUNT, 0, 0)

then add your text

richtextbox.appendtext("your text here")

you will know how many lines to scroll down with by running the
EM_GETLINECOUNT after you added your new text like so

Private intLinesToAdd as integer = (SendMessage(richtextbox.Handle,
EM_GETLINECOUNT, 0, 0) - intTotalLines)

Now that you know how many lines have been added...you know how many
lines your scroll bar needs to scroll down.
We use this line to scroll the scroll bar down...

SendMessage(richtextbox.Handle, EM_LINESCROLL, 0, intLinesToAdd)

I've changed it a little from my original code to make it easier for
you to use...I hope it still works how it's suppose to. If not I
think you get the jist of it.
JonnyT

Nov 20 '05 #3

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

Similar topics

6
by: Corepaul | last post by:
I am new to Access 2000. My operating system is Windows 2000. In the early stage of development I noticed something weird. On my form, I have a Command Button named "btnAlbumUp". The first time...
5
by: Wilfried Mestdagh | last post by:
Hello, textBox += someData + "\r\n"; does not scroll the visible text to the end. How do I perform that ? Also this way seems to me a lot of reallocating memory. Is there better way to add...
3
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...
0
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...
0
by: 23s | last post by:
Is there any way I can send a vertical value to a form's scroll position? I have a full-screen form that, at launch, contains an empty tab sheet. At run time, the user can dynamically append a...
3
by: Al | last post by:
Hello, How can I turn off auto scrolling in RIchTextBox? Thanks
0
by: Terry Olsen | last post by:
The following code works fine in a TextBox for auto-scrolling: If AutoScroll = True Then txtChat.Select(txtChat.Text.Length, 0) txtChat.ScrollToCaret() End If If I set AutoScroll to True...
1
by: dotnetnoob | last post by:
i have a Dialog form with richtextbox that basically display a legal agreement and when the user drag the scroll thumb down to the end of scrolling which suppose to mean the user have read it then...
2
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.