Results 1 to 10 of 10

Thread: Find text in RTB is very slow ?[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Find text in RTB is very slow ?[Resolved]

    This method works but it's damn slow . It takes about 1 minute to colorize some keywords . I feel like there is a way to improve it and speed it up . Anyone has a solution or alternative method ?

    VB Code:
    1. Public Sub FindHighlight(ByVal Keywords As ArrayList, ByVal HighlightColor As Color)
    2.         Me.SuspendLayout()
    3.         Dim StartLooking As Integer = 0
    4.         Dim FoundAt As Integer
    5.         Dim SearchLength As Integer        
    6.  
    7.         For Each keyword As String In Keywords
    8.  
    9.             SearchLength = keyword.Length
    10.             Application.DoEvents()
    11.             Do
    12.                 FoundAt = Me.RichTextBox1.Find(keyword, StartLooking, RichTextBoxFinds.WholeWord)
    13.                 If FoundAt > -1 Then Me.RichTextBox1.SelectionColor = HighlightColor
    14.                 StartLooking = StartLooking + SearchLength
    15.             Loop While FoundAt > -1
    16.             SearchLength = 0
    17.             FoundAt = 0
    18.             StartLooking = 0
    19.         Next
    20.  
    21.         Me.ResumeLayout()
    22.     End Sub

    btw , I'll not go with threading now .It's my last option , I think


    Thanks
    Last edited by Pirate; May 23rd, 2004 at 10:30 AM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    use Regex Pirate mate
    it's great on Keywords , you can use the Index and Length properties on the Match to select / highlight / carry on to the next found item. it's about the fastest i've found for highlighting in RTF.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Any example , demo .. I've never tried regex before

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    a very simple example ( cuz it's sunday and we been having a family day ) ...
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim keywords() As String = {"public", "private", "void", "class", "string"}
    3.         For Each keyword As String In keywords
    4.             Dim rgx As New Regex(keyword & "\s*([^a-z,0-9]+)", RegexOptions.IgnoreCase)
    5.             Dim mCol As MatchCollection = rgx.Matches(rtfSyntax.Text)
    6.             For Each m As Match In mCol
    7.                 rtfSyntax.Select(m.Index, m.Length)
    8.                 rtfSyntax.SelectionColor = Color.Blue
    9.             Next
    10.         Next
    11.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    God bless you and your family .

    I got the idea and I can go on with my project .

  6. #6
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Great example. You should work up a small sample project and put it in the .Net Code Bank.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    sysop , small glitch in the pattern applied !!

    It extends the color to some chars like (# , () in tostring() function ) . It doesn't seem to be %100 accurate . And also , how can I make it differenciate between string and tostring (it colorize both).

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    like i said , a basic example. you need to specify if you want spaces before / after a string with regex
    \s+private\s+ would search for the word " private " with a space before and after it , but it will prevent you from finding all cases of the word ( eg: if you are at the begining or end of a row of text )
    to search for the word but not allow chars before or after ( eg: privateW fails , wPrivate fails , private succeeds ) ...
    PHP Code:
    "\s*([^a-z,0-9]+)" keyword "\s*([^a-z,0-9]+)" 
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Using keyword as a pattern works . Thanks for explanation though .

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    PHP Code:
    "\s" keyword "\s" 
    This works better , but I'm having trouble with comment lines . I don't want to highlight keywords in comments . How can I skip like this " # region " or " // private fields ...etc" ??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width