Results 1 to 5 of 5

Thread: [RESOLVED] quick search in a richtextbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Resolved [RESOLVED] quick search in a richtextbox

    Hi,

    I have a RichTextBox where I expect 100 characters per line. Each line can start with the letter A, B, or C. I may have thousands of lines and most of them will start with the letter C.

    When the cursor is moved from one line to another and is placed on a "C" line, I need to be able to quickly locate the nearest "B" line above it. It may be hundreds of lines away. A loop to search for the "B" line using GetFirstCharIndexFromLine(x) is too slow.

    The user may want to paste many lines into the textbox. If necessary, a mild wait after pasting is acceptable.

    Do you have any suggestions for a technique to accomplish what I need to do? Even just a high level suggestion like "use a linked list" would be appreciated.

    Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: quick search in a richtextbox

    which version of vb.net are you using?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: quick search in a richtextbox

    2008 or 2010.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: quick search in a richtextbox

    try this:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim currentLine As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
    3.  
    4.     Dim B_lines = (From line In RichTextBox1.Lines _
    5.                          Where line.StartsWith("B") _
    6.                          Select Array.IndexOf(RichTextBox1.Lines, line) _
    7.                          ).ToArray
    8.     Dim nearest_B_line = (From index In B_lines _
    9.                          Where index < currentLine _
    10.                          Select index _
    11.                          ).ToArray
    12.  
    13.     If nearest_B_line.Length > 0 Then
    14.         MsgBox("currentLine" & currentLine & Environment.NewLine & "nearest_B_line" & nearest_B_line(nearest_B_line.GetUpperBound(0)))
    15.     Else
    16.         MsgBox("no B lines before current line")
    17.     End If
    18. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: quick search in a richtextbox

    paul, that's exactly what i needed. it's working real well. thanks so much!

Tags for this Thread

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