2010-12-06 3 views
2

Comment puis-je créer une fonction de recherche suivante? De mes codes actuels, si un mot de textBoxSearch est trouvé richTextBoxBrowsing, le mot serait mis en surbrillance à l'intérieur du richTextBoxBrowsing.C# Création d'une fonction de recherche suivante

Mais s'il y a plus d'un mot trouvé, je ne peux voir que le premier. Par conséquent, je voudrais appuyer sur le bouton F3 pour trouver le mot suivant et il serait mis en évidence un par un jusqu'à la fin de richTextBoxBrowsing.

Merci d'avance!

 String s1 = textBoxSearch.Text.ToLower(); 
     int startPos = richTextBoxBrowsing.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!"); 
     } 

Répondre

3

L'astuce est de garder la main du dernier indice connu (la dernière valeur que vous avez pour startPos) - peut-être dans un champ au niveau de la forme, vous pouvez utiliser:

int startPos = Find(s1, lastIndex + 1, RichTextBoxFinds.None); 

(où lastIndex de -1 le fera démarrer au début)

+0

J'utiliser 'lastIndex + s1.Length'. –

+0

Mais que se passe-t-il si le second résultat de recherche est dans le premier, comme dans certaines expressions régulières? Certes cela n'utilise pas encore le programme Regexs, mais c'est toujours utile ... – Miguel

2

Vous devez enregistrer l'état de la recherche précédente, par exemple le rappel de l'index de l'élément précédemment trouvé. Chaque fois que la chaîne de recherche change, vous réinitialisez l'index de départ à -1.

1

Voici ce que j'ai pour une fonction "Rechercher Suivant". C'est sur VB.net car je travaille actuellement sur un projet TAFE mais vous pouvez facilement le convertir en C#. Cela fonctionne à merveille pour moi.

J'ai une zone richtext principale appelée 'RichTextBox1' où le texte est, puis j'ai une zone de texte appelée 'ToolStripSearchTextBox' où j'entrer ce que je veux rechercher et un bouton appelé 'ToolStripButton2' qui appelle la méthode 'FindNext_Click() 'lorsque cliqué.

Cette fonction "Rechercher suivant" n'est pas sensible à la casse en raison de 'RichTextBoxFinds.None'. N'hésitez pas à changer cela comme vous le souhaitez.

// 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 
Questions connexes