RichTextBox Slow 
Author Message
 RichTextBox Slow

My VB6 app needs to position the caret in the text and highlight a word.
If there is a large (define large) amount of text then it takes around 350
mSec to do the task.  This is way too slow.  It takes 0 to 10 mSec when a few
lines are in the RTB.
So I tried to use the SendMessage but it simply fails to position the caret.
What am I doing wrong?
Is there better way to get a fast RTB.
I hate the idea and the coding overhead to page text in and out of a RTB to
have smaller contents to make it work.

Also need an API SelLength but cannot figure out the parameter.

' this failes to move the caret
Private Const WM_USER = &H400
Private Const EM_EXSETSEL = (WM_USER + 55)
Private Declare Function SendMessageX Lib "USER32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Long) As Long

Public Function RTBSelStart(RTB As RichTextBox, ByVal lCharPos As Long) As
Long

    On Error GoTo RTBSelStartErr

    SendMessageX RTB.hwnd, EM_EXSETSEL, 0, lCharPos

RTBSelStartExit:
    Exit Function

RTBSelStartErr:
    Debug.Assert False
    Resume RTBSelStartExit

End Function 'RTBSelStart



Sat, 18 Feb 2012 08:08:01 GMT  
 RichTextBox Slow

Quote:

> My VB6 app needs to position the caret in the text and highlight a word.
> If there is a large (define large) amount of text then it takes around 350
> mSec to do the task.  This is way too slow.  It takes 0 to 10 mSec when a
> few
> lines are in the RTB.

Is the delay with finding the word to highlight, or with the positioning of
the caret?

Quote:
> So I tried to use the SendMessage but it simply fails to position the
> caret.
> What am I doing wrong?
> Is there better way to get a fast RTB.
> I hate the idea and the coding overhead to page text in and out of a RTB
> to
> have smaller contents to make it work.

> Also need an API SelLength but cannot figure out the parameter.>

Try this,
Private Declare Function SendMessage Lib "USER32" _
        Alias "SendMessageA" ( _
        ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long

Private Type CHARRANGE
  cpMin As Long
  cpMax As Long
End Type

Private Const WM_USER = &H400
Private Const EM_EXSETSEL = (WM_USER + 55)

Private Sub Command1_Click()
    Dim tCharRange As CHARRANGE
    tCharRange.cpMin = 10   'start of selection
    tCharRange.cpMax = 19   'end of selection
    SendMessage RTB.hwnd, EM_EXSETSEL, 0, tCharRange
    RTB.SetFocus
End Sub

Quote:
> ' this failes to move the caret
> Private Const WM_USER = &H400
> Private Const EM_EXSETSEL = (WM_USER + 55)
> Private Declare Function SendMessageX Lib "USER32" Alias "SendMessageA" _
>    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam
> As
> Long) As Long

> Public Function RTBSelStart(RTB As RichTextBox, ByVal lCharPos As Long) As
> Long

>    On Error GoTo RTBSelStartErr

>    SendMessageX RTB.hwnd, EM_EXSETSEL, 0, lCharPos

> RTBSelStartExit:
>    Exit Function

> RTBSelStartErr:
>    Debug.Assert False
>    Resume RTBSelStartExit

> End Function 'RTBSelStart



Sat, 18 Feb 2012 09:17:10 GMT  
 RichTextBox Slow
The time does not include the locating just the SelStart and SelLength.
I know the location and length when I call the sub to highlight the word.
I will study your code.
Thanks.
Quote:


> > My VB6 app needs to position the caret in the text and highlight a word.
> > If there is a large (define large) amount of text then it takes around 350
> > mSec to do the task.  This is way too slow.  It takes 0 to 10 mSec when a
> > few
> > lines are in the RTB.

> Is the delay with finding the word to highlight, or with the positioning of
> the caret?

> > So I tried to use the SendMessage but it simply fails to position the
> > caret.
> > What am I doing wrong?
> > Is there better way to get a fast RTB.
> > I hate the idea and the coding overhead to page text in and out of a RTB
> > to
> > have smaller contents to make it work.

> > Also need an API SelLength but cannot figure out the parameter.>

> Try this,
> Private Declare Function SendMessage Lib "USER32" _
>         Alias "SendMessageA" ( _
>         ByVal hwnd As Long, _
>         ByVal wMsg As Long, _
>         ByVal wParam As Long, _
>         lParam As Any) As Long

> Private Type CHARRANGE
>   cpMin As Long
>   cpMax As Long
> End Type

> Private Const WM_USER = &H400
> Private Const EM_EXSETSEL = (WM_USER + 55)

> Private Sub Command1_Click()
>     Dim tCharRange As CHARRANGE
>     tCharRange.cpMin = 10   'start of selection
>     tCharRange.cpMax = 19   'end of selection
>     SendMessage RTB.hwnd, EM_EXSETSEL, 0, tCharRange
>     RTB.SetFocus
> End Sub

> > ' this failes to move the caret
> > Private Const WM_USER = &H400
> > Private Const EM_EXSETSEL = (WM_USER + 55)
> > Private Declare Function SendMessageX Lib "USER32" Alias "SendMessageA" _
> >    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam
> > As
> > Long) As Long

> > Public Function RTBSelStart(RTB As RichTextBox, ByVal lCharPos As Long) As
> > Long

> >    On Error GoTo RTBSelStartErr

> >    SendMessageX RTB.hwnd, EM_EXSETSEL, 0, lCharPos

> > RTBSelStartExit:
> >    Exit Function

> > RTBSelStartErr:
> >    Debug.Assert False
> >    Resume RTBSelStartExit

> > End Function 'RTBSelStart



Sat, 18 Feb 2012 09:31:01 GMT  
 RichTextBox Slow
Got your code working but still too slow.
Your code is about twice as fast as the standard .SelStart and .SelLength.
This will help if I have to do paging, less paging.
For short RTB text is will be fine.
I have no control over the speed I get the start and length so I have to
figure out something.
I am working on a laptop that is reasonably fast but should have my code
work on a slower PC too.

I hate to wonder about using RichEd20.dll.  Is it much faster and worth the
effort?
See previous post re multi-undo.
I have it working well with VB's richtextbox.

Quote:


> > My VB6 app needs to position the caret in the text and highlight a word.
> > If there is a large (define large) amount of text then it takes around 350
> > mSec to do the task.  This is way too slow.  It takes 0 to 10 mSec when a
> > few
> > lines are in the RTB.

> Is the delay with finding the word to highlight, or with the positioning of
> the caret?

> > So I tried to use the SendMessage but it simply fails to position the
> > caret.
> > What am I doing wrong?
> > Is there better way to get a fast RTB.
> > I hate the idea and the coding overhead to page text in and out of a RTB
> > to
> > have smaller contents to make it work.

> > Also need an API SelLength but cannot figure out the parameter.>

> Try this,
> Private Declare Function SendMessage Lib "USER32" _
>         Alias "SendMessageA" ( _
>         ByVal hwnd As Long, _
>         ByVal wMsg As Long, _
>         ByVal wParam As Long, _
>         lParam As Any) As Long

> Private Type CHARRANGE
>   cpMin As Long
>   cpMax As Long
> End Type

> Private Const WM_USER = &H400
> Private Const EM_EXSETSEL = (WM_USER + 55)

> Private Sub Command1_Click()
>     Dim tCharRange As CHARRANGE
>     tCharRange.cpMin = 10   'start of selection
>     tCharRange.cpMax = 19   'end of selection
>     SendMessage RTB.hwnd, EM_EXSETSEL, 0, tCharRange
>     RTB.SetFocus
> End Sub

> > ' this failes to move the caret
> > Private Const WM_USER = &H400
> > Private Const EM_EXSETSEL = (WM_USER + 55)
> > Private Declare Function SendMessageX Lib "USER32" Alias "SendMessageA" _
> >    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam
> > As
> > Long) As Long

> > Public Function RTBSelStart(RTB As RichTextBox, ByVal lCharPos As Long) As
> > Long

> >    On Error GoTo RTBSelStartErr

> >    SendMessageX RTB.hwnd, EM_EXSETSEL, 0, lCharPos

> > RTBSelStartExit:
> >    Exit Function

> > RTBSelStartErr:
> >    Debug.Assert False
> >    Resume RTBSelStartExit

> > End Function 'RTBSelStart



Sat, 18 Feb 2012 09:42:03 GMT  
 RichTextBox Slow
  Using RichEd20 will be somewhat faster because
you're cutting out an external middleman. With the
control your messages have to go to the OCX, which
then wraps the DLL. If you use Riched20 directly
then you've cut out the step of the OCX.

    But it still shouldn't be anywhere near that slow.
I add syntax highlighting to code in an RTB by rebuilding
the richtext content. I find that I can re-color  in under
1/4 second if the file is not too big. (That includes getting
the content as plain text, running it through a tokenizing
function to build a richtext version with appropriate coloring
for keywords, comments, variables, etc., then putting the
richtext string back into the RTB.)  So something is very
wrong if you can't select text in an amount of time so
small that you probably can't even measure it. You might
want to try running Spy++ to see what's wrong. And try
turning off things like AV that might possibly be locking
things up. Also avoid using the Change event. That can
bog things down.

Quote:
> Got your code working but still too slow.
> Your code is about twice as fast as the standard .SelStart and .SelLength.
> This will help if I have to do paging, less paging.
> For short RTB text is will be fine.
> I have no control over the speed I get the start and length so I have to
> figure out something.
> I am working on a laptop that is reasonably fast but should have my code
> work on a slower PC too.

> I hate to wonder about using RichEd20.dll.  Is it much faster and worth
the
> effort?
> See previous post re multi-undo.
> I have it working well with VB's richtextbox.



> > > My VB6 app needs to position the caret in the text and highlight a
word.
> > > If there is a large (define large) amount of text then it takes around
350
> > > mSec to do the task.  This is way too slow.  It takes 0 to 10 mSec
when a
> > > few
> > > lines are in the RTB.

> > Is the delay with finding the word to highlight, or with the positioning
of
> > the caret?

> > > So I tried to use the SendMessage but it simply fails to position the
> > > caret.
> > > What am I doing wrong?
> > > Is there better way to get a fast RTB.
> > > I hate the idea and the coding overhead to page text in and out of a
RTB
> > > to
> > > have smaller contents to make it work.

> > > Also need an API SelLength but cannot figure out the parameter.>

> > Try this,
> > Private Declare Function SendMessage Lib "USER32" _
> >         Alias "SendMessageA" ( _
> >         ByVal hwnd As Long, _
> >         ByVal wMsg As Long, _
> >         ByVal wParam As Long, _
> >         lParam As Any) As Long

> > Private Type CHARRANGE
> >   cpMin As Long
> >   cpMax As Long
> > End Type

> > Private Const WM_USER = &H400
> > Private Const EM_EXSETSEL = (WM_USER + 55)

> > Private Sub Command1_Click()
> >     Dim tCharRange As CHARRANGE
> >     tCharRange.cpMin = 10   'start of selection
> >     tCharRange.cpMax = 19   'end of selection
> >     SendMessage RTB.hwnd, EM_EXSETSEL, 0, tCharRange
> >     RTB.SetFocus
> > End Sub

> > > ' this failes to move the caret
> > > Private Const WM_USER = &H400
> > > Private Const EM_EXSETSEL = (WM_USER + 55)
> > > Private Declare Function SendMessageX Lib "USER32" Alias
"SendMessageA" _
> > >    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
lParam
> > > As
> > > Long) As Long

> > > Public Function RTBSelStart(RTB As RichTextBox, ByVal lCharPos As
Long) As
> > > Long

> > >    On Error GoTo RTBSelStartErr

> > >    SendMessageX RTB.hwnd, EM_EXSETSEL, 0, lCharPos

> > > RTBSelStartExit:
> > >    Exit Function

> > > RTBSelStartErr:
> > >    Debug.Assert False
> > >    Resume RTBSelStartExit

> > > End Function 'RTBSelStart



Sat, 18 Feb 2012 11:43:35 GMT  
 RichTextBox Slow
You nailed it!

Background:  I errored before.  The time to SelStart and SelLength ws 650
mSec.
It dropped to 320 mSec when I used the SnedMessage code.

I never would have thought to look at Change or SelChange.
I am using SelChange and Change.

But I do need Change and SelChange since the RichTExtBox needs editing
capability.

SelChange called a sub to set some parameter but not necessary during this
part of the operation so I can flag to skip the sub call while doing the
intensive stuff.

So I am now learning the hard way what SelChange really does.
I thought it was for things like bold, italic etc as shown in MS examples.  
I did not think it was for something like SelLength.

I did not see it when I stepped but when I run and put a break there it sure
gets into SelChange.
I commented out the call and the time from GetTickCounter is 0 mSec delta
across the SelStart and SelLength.
Nice.
Thanks a lot.

I've got to learn to use Spy++ but there are no tutors around me any more.

Quote:

>   Using RichEd20 will be somewhat faster because
> you're cutting out an external middleman. With the
> control your messages have to go to the OCX, which
> then wraps the DLL. If you use Riched20 directly
> then you've cut out the step of the OCX.

>     But it still shouldn't be anywhere near that slow.
> I add syntax highlighting to code in an RTB by rebuilding
> the richtext content. I find that I can re-color  in under
> 1/4 second if the file is not too big. (That includes getting
> the content as plain text, running it through a tokenizing
> function to build a richtext version with appropriate coloring
> for keywords, comments, variables, etc., then putting the
> richtext string back into the RTB.)  So something is very
> wrong if you can't select text in an amount of time so
> small that you probably can't even measure it. You might
> want to try running Spy++ to see what's wrong. And try
> turning off things like AV that might possibly be locking
> things up. Also avoid using the Change event. That can
> bog things down.

> > Got your code working but still too slow.
> > Your code is about twice as fast as the standard .SelStart and .SelLength.
> > This will help if I have to do paging, less paging.
> > For short RTB text is will be fine.
> > I have no control over the speed I get the start and length so I have to
> > figure out something.
> > I am working on a laptop that is reasonably fast but should have my code
> > work on a slower PC too.

> > I hate to wonder about using RichEd20.dll.  Is it much faster and worth
> the
> > effort?
> > See previous post re multi-undo.
> > I have it working well with VB's richtextbox.



> > > > My VB6 app needs to position the caret in the text and highlight a
> word.
> > > > If there is a large (define large) amount of text then it takes around
> 350
> > > > mSec to do the task.  This is way too slow.  It takes 0 to 10 mSec
> when a
> > > > few
> > > > lines are in the RTB.

> > > Is the delay with finding the word to highlight, or with the positioning
> of
> > > the caret?

> > > > So I tried to use the SendMessage but it simply fails to position the
> > > > caret.
> > > > What am I doing wrong?
> > > > Is there better way to get a fast RTB.
> > > > I hate the idea and the coding overhead to page text in and out of a
> RTB
> > > > to
> > > > have smaller contents to make it work.

> > > > Also need an API SelLength but cannot figure out the parameter.>

> > > Try this,
> > > Private Declare Function SendMessage Lib "USER32" _
> > >         Alias "SendMessageA" ( _
> > >         ByVal hwnd As Long, _
> > >         ByVal wMsg As Long, _
> > >         ByVal wParam As Long, _
> > >         lParam As Any) As Long

> > > Private Type CHARRANGE
> > >   cpMin As Long
> > >   cpMax As Long
> > > End Type

> > > Private Const WM_USER = &H400
> > > Private Const EM_EXSETSEL = (WM_USER + 55)

> > > Private Sub Command1_Click()
> > >     Dim tCharRange As CHARRANGE
> > >     tCharRange.cpMin = 10   'start of selection
> > >     tCharRange.cpMax = 19   'end of selection
> > >     SendMessage RTB.hwnd, EM_EXSETSEL, 0, tCharRange
> > >     RTB.SetFocus
> > > End Sub

> > > > ' this failes to move the caret
> > > > Private Const WM_USER = &H400
> > > > Private Const EM_EXSETSEL = (WM_USER + 55)
> > > > Private Declare Function SendMessageX Lib "USER32" Alias
> "SendMessageA" _
> > > >    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
> lParam
> > > > As
> > > > Long) As Long

> > > > Public Function RTBSelStart(RTB As RichTextBox, ByVal lCharPos As
> Long) As
> > > > Long

> > > >    On Error GoTo RTBSelStartErr

> > > >    SendMessageX RTB.hwnd, EM_EXSETSEL, 0, lCharPos

> > > > RTBSelStartExit:
> > > >    Exit Function

> > > > RTBSelStartErr:
> > > >    Debug.Assert False
> > > >    Resume RTBSelStartExit

> > > > End Function 'RTBSelStart



Sat, 18 Feb 2012 14:02:01 GMT  
 RichTextBox Slow

Quote:
> You nailed it!

> Background:  I errored before.  The time to SelStart and SelLength ws 650
> mSec.
> It dropped to 320 mSec when I used the SnedMessage code.

> I never would have thought to look at Change or SelChange.
> I am using SelChange and Change.

   I thought of that because I ran into it once, finally
discovering that the change event fires for virtually
everything, thereby doubling the number of events,
and slowing things to a crawl if one puts any code
into the event sub.

Quote:
> But I do need Change and SelChange since the RichTExtBox needs editing
> capability.

  You should be able to find a way to work around that.
Maybe that's part of the Undo issue?  It *is* hard to
make a good multi-Undo function in a v. 1 RTB. First
you've got a potential disaster if you're going to store
30-100 Undos and someone decides to load a 2 MB
file. Second is the problem you're facing: If you want
to track more than cut and paste then it becomes very
work intensive. A v. 2+ RTB does all that internally and
seems to do it in some sort of very efficient way, storing
change data, rather than whole copies of the loaded file,
each time a character is typed in.


Sat, 18 Feb 2012 23:07:09 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Richtextbox slow in selecting text

2. Is the RichTextBox slow ?

3. LoadFile to RichTextBox to slow

4. Slow, slow, slow search on VB6, ADO and SS7

5. VB4 is slow..slow....slow

6. Insert into SQL Server slow, slow, slow...

7. SLOW - ADO ASP FOXPRO CDX - SLOW

8. It gets slower and slower.....

9. Echo RichTextBox to RichTextBox

10. This routine runs slower slower each time it's used. Any Ideas why?

11. Slow form_load due to slow MSADORA connection: how does one deal with this?

12. FindFirst is slow

 

 
Powered by phpBB® Forum Software