VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Super fast append to a RichTextBox control. As the RichTextBox gets larger, the traditional append

by Austin Leo Chambers (1 Submission)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 23rd July 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Super fast append to a RichTextBox control. As the RichTextBox gets larger, the traditional append operation gets slower. This code

Rate Super fast append to a RichTextBox control. As the RichTextBox gets larger, the traditional append



'   to use the RichTextBox because of the darn 64K character limit in the
'   TextBox.  My task was to find a fast and practical way to append
'   strings to a RichTextBox.
'
'Author: Austin Leo Chambers (2003)


'This function represents the "brute force" way of writing to a text box.
'   Accordingly, it is quite slow.  On the Microsoft performance chart,
'   %Processor Time quickly rose as the RichTextBox got large.  Setting
'   a timer to execute this code every 50ms quickly demanded massive
'   CPU time.
Public Sub AddStringSlowest(YourString As String)
    RichTextBox1.Text = RichTextBox1.Text & YourString & vbCrLf
End Sub

'On the Microsoft performance chart, this subroutine did not raise
'   %Processor Time as quickly as the previous.  However, the
'   RichTextBox again began to gradually demand more CPU time
'   as the RichTextBox got larger.  Setting a timer to execute
'   this code every 50ms made this program gradually require
'   more and more CPU time.  Eventually, this subroutine began
'   to take unreasonably long.
'
'I believe this is due to the Len function.  As the RichTextBox
'   gets larger, the Len function has to do more work.
Public Sub AddStringSlow(YourString As String)
    RichTextBox1.SelStart = Len(RichTextBox.Text)
    RichTextBox1.SelText = YourString & vbCrLf
End Sub

'Taking advantage of the fact that "Setting SelStart greater than the
'   text length sets the property to the existing text length," I decided
'   to replace the Len statement with a constant.
'
'The following code does not demand more CPU time as the RichTextBox
'   increases in size.  In fact, A timer to execute this code every
'   50ms had %Processor Time remain at 0-1%, and it stayed at 0-1%
'This is the fastest way to append text to a RichTextBox, making it
'   essentially as practical as a textbox, but without the annoying 64K
'   limitation.
Public Sub AddStringFastest(YourString As String)
    RichTextBox1.SelStart = 2000000000
    RichTextBox1.SelText = YourString & vbCrLf
End Sub

Download this snippet    Add to My Saved Code

Super fast append to a RichTextBox control. As the RichTextBox gets larger, the traditional append Comments

No comments have been posted about Super fast append to a RichTextBox control. As the RichTextBox gets larger, the traditional append . Why not be the first to post a comment about Super fast append to a RichTextBox control. As the RichTextBox gets larger, the traditional append .

Post your comment

Subject:
Message:
0/1000 characters