[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.
Re: quick search in a richtextbox
which version of vb.net are you using?
Re: quick search in a richtextbox
Re: quick search in a richtextbox
try this:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim currentLine As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim B_lines = (From line In RichTextBox1.Lines _
Where line.StartsWith("B") _
Select Array.IndexOf(RichTextBox1.Lines, line) _
).ToArray
Dim nearest_B_line = (From index In B_lines _
Where index < currentLine _
Select index _
).ToArray
If nearest_B_line.Length > 0 Then
MsgBox("currentLine" & currentLine & Environment.NewLine & "nearest_B_line" & nearest_B_line(nearest_B_line.GetUpperBound(0)))
Else
MsgBox("no B lines before current line")
End If
End Sub
Re: quick search in a richtextbox
paul, that's exactly what i needed. it's working real well. thanks so much!