C# Creating a find next function
How can I create a find next function?
From my current codes, if a word from textBoxSearch is found richTextBoxBrowsing, the word would be highlighted inside the richTextBoxBrowsing.
But if there are more than 1 of the same word found, I can only see the first one. Hence, I would like to press the button F3 to find the next word and it would be highlighted one by one until the end of richTextBoxBrowsing.
Thanks in advance!
String s1 = textBoxSearch.Text.ToLower();
int startPos = richTex开发者_如何转开发tBoxBrowsing.Find(s1);
int length = s1.Length;
if (startPos > -1)
{
MessageBox.Show("Word found!");
richTextBoxBrowsing.Focus();
richTextBoxBrowsing.Select(startPos, length);
}
else
{
MessageBox.Show("Word not found!");
}
The trick is to keep hold of the last known index (i.e. the last value you got for startPos
) - perhaps in a form-level field, then you can use:
int startPos = Find(s1, lastIndex + 1, RichTextBoxFinds.None);
(where a lastIndex
of -1 will cause it to start at the beginning)
You'd have to save the state of the previous search, for instance the remember the index of the previously found item. Whenever the search string changes, you reset the starting index to -1.
Here's what I've got for a "Find Next" function. It's in VB.net as I'm currently working on a TAFE project but you can easily convert it to C#. It works wonders for me.
I have a main richtextbox called 'RichTextBox1' where the text is, then I have a textbox called 'ToolStripSearchTextBox' where I input what I want to search and a button called 'ToolStripButton2' that calls the method 'FindNext_Click()' when clicked.
This "Find Next" function is not case-sensitive due to 'RichTextBoxFinds.None'. Feel free to change that as you wish.
// Find next
Dim searchIndex As Integer = 0
Dim lastSearch As String
Private Sub FindNext_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
// If the search textbox is empty, focus on it
If (ToolStripSearchTextBox.Text = String.Empty) Then
ToolStripSearchTextBox.Focus()
Return
End If
// If user changed their search term, reset the index
If (ToolStripSearchTextBox.Text <> lastSearch) Then
searchIndex = 0
End If
lastSearch = ToolStripSearchTextBox.Text
// If the character(s) exist, update the index. Otherwise, set the index to -1
Try
searchIndex = RichTextBox1.Find(ToolStripSearchTextBox.Text, searchIndex, RichTextBoxFinds.None)
Catch ex As ArgumentOutOfRangeException
searchIndex = -1
End Try
// Character(s) exists, focus on the main textbox and then select the character(s)
If (searchIndex <> -1) Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = searchIndex
RichTextBox1.SelectionLength = ToolStripSearchTextBox.Text.Length
searchIndex = searchIndex + 1
Else // No occurances of text or user has highlghted last remaining word. Let the user know they have reached the end of the document and reset the index
searchIndex = 0
//RichTextBox1.SelectionStart = 0
//RichTextBox1.SelectionLength = 0
MessageBox.Show("End of document", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
精彩评论