473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.sel ectionstart = richtextbox.tex tlength
richtextbox.scr olltocaret()

I think that should do it...can't remember offhand...somet hing 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("s ometext") method. That's what it's there for.
Then you will need the sendmessage API...

Public Declare Function SendMessage Lib "user32.dll " Alias _
"SendMessag eA" (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(ric htextbox.Handle ,
EM_GETLINECOUNT , 0, 0)

then add your text

richtextbox.app endtext("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(ri chtextbox.Handl e,
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(ric htextbox.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 17369
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.sel ectionstart = richtextbox.tex tlength
richtextbox.scr olltocaret()

I think that should do it...can't remember offhand...somet hing 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("so metext") method. That's what it's there for.
Then you will need the sendmessage API...

Public Declare Function SendMessage Lib "user32.dll " Alias _
"SendMessag eA" (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(ric htextbox.Handle ,
EM_GETLINECOUNT , 0, 0)

then add your text

richtextbox.app endtext("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(ri chtextbox.Handl e,
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(ric htextbox.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.sel ectionstart = richtextbox.tex tlength
richtextbox.scr olltocaret()

I think that should do it...can't remember offhand...somet hing 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("so metext") method. That's what it's there for.
Then you will need the sendmessage API...

Public Declare Function SendMessage Lib "user32.dll " Alias _
"SendMessag eA" (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(ric htextbox.Handle ,
EM_GETLINECOUNT , 0, 0)

then add your text

richtextbox.app endtext("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(ri chtextbox.Handl e,
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(ric htextbox.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
10234
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 that I reference this button in VBA code, the Visual Basic Editor capitalizes the P changing btnAlbumUp.SetFocus to
5
31006
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 text to a textbox ? -- rgds, Wilfried
3
9410
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 clicks. If the user slides the "thumb", I seem to get no indication. Anyone know how to get notified of the thumb slide from a RichTextBox? I can derive from RichTextBox and trap messages if need be, but I have not been able to figure out what to...
0
329
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 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...
0
2245
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 rich textbox to the tab sheet. The rich textbox is placed at 0,0 (upper left corner) within the tab sheet and is initially 1 "line" tall to accomodate the user's first line of text. The rich text box is, in actuality, a user control of mine...
3
1745
by: Al | last post by:
Hello, How can I turn off auto scrolling in RIchTextBox? Thanks
0
1385
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 then when text is appended to the TextBox, it scrolls down to show it if necessary. When I set AutoScroll to false, it doesn't scroll, allowing me to read whatever is currently visible.
1
2899
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 the checkbox is enabled for the user to tick off (I agree) and the next button is enabled for user to click next. the richtextbox vscroll event only fire when user click on the scrollthumb and that's it. how do i implement it when user drag the...
2
9778
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 the user changes the input data, a recalculation is made and a new report is generated. This resets the RichTextBox vertical scroll bar to the top. I would like to be able to read the position the scroll bar is moved to before recalculation....
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3300
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 we have to send another system

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.