473,394 Members | 1,916 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,394 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 17245
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.