PC Review


Reply
Thread Tools Rate Thread

How to auto scroll a richtextbox in Visual Basic .Net

 
 
JonnyT
Guest
Posts: n/a
 
      7th Apr 2004
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
 
Reply With Quote
 
 
 
 
Cor
Guest
Posts: n/a
 
      7th Apr 2004
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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouse won't scroll in Visual Basic Editor =?Utf-8?B?U2V0aCBTY2h3YXJt?= Windows XP Hardware 3 8th Nov 2005 02:48 AM
RichTextBox auto scroll to end =?Utf-8?B?QmluIFhpbmc=?= Microsoft Dot NET Framework Forms 4 28th Jun 2005 07:29 PM
Mouse Wheel Won't Scroll in Visual Basic Editor. Do This SidBord Microsoft Excel Misc 1 10th Jul 2004 12:45 PM
Solution to Mouse Wheel Won't Scroll in Visual Basic Editor SidBord Microsoft Excel Programming 1 7th Jul 2004 03:15 AM
Microsoft Visual Basic Editor and Mouse Scroll ndp Microsoft Excel Programming 4 12th Dec 2003 02:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:18 PM.