Question

Je souhaiterais effectuer une recherche vers le haut, le bas et la casse, si possible. Même les liens pour me lancer seraient appréciés.

Était-ce utile?

La solution

Vous n'êtes pas sûr de la recherche mais vous pouvez utiliser quelque chose comme ceci

int selStart = ControltoSearch.SelectionStart;
int selLength = ControltoSearch.SelectionLength;
int newLength = SearchFor.Length;

int newStart = searchIn.IndexOf(SearchFor, selStart + selLength, compareType);

ControltoSearch.SelectionStart = newStart >= 0 ? newStart : 0;
ControltoSearch.SelectionLength = newLength;
ControltoSearch.ScrollToCaret();
ControltoSearch.Focus();

return newStart;

En cas de correspondance, vous pouvez utiliser String.ToLowerInvariant () pour la recherche de texte et la recherche de texte, sinon String.Contains () est sensible à la casse

searchIn.ToLowerInvariant().Contains(SearchFor.ToLowerInvariant())

Autres conseils

Vous pouvez utiliser l'option "Rechercher". méthode sur la zone de texte enrichi lui-même.

Si vous configurez un formulaire avec une case à cocher pour "Match Case". et une case à cocher pour "Rechercher vers le haut". et avez ajouté une propriété sur votre formulaire de recherche appelée ControlToSearch qui intègre un contrôle RichTextBox, vous pouvez effectuer les opérations suivantes:

RichTextBoxFinds options = RichTextBoxFinds.None;

int from = ControlToSearch.SelectionStart;
int to = ControlToSearch.TextLength - 1;

if (chkMatchCase.Checked)
{
    options = options | RichTextBoxFinds.MatchCase;
}
if (chkSearchUp.Checked)
{
    options = options | RichTextBoxFinds.Reverse;
    to = from;
    from = 0;
}

int start = 0;
start = ControlToSearch.Find(txtSearchText.Text, from, to, options);

if (start > 0)
{
    ControlToSearch.SelectionStart = start;
    ControlToSearch.SelectionLength = txtSearchText.TextLength;
    ControlToSearch.ScrollToCaret();
    ControlToSearch.Refresh();
    ControlToSearch.Focus();
}
else
{
    MessageBox.Show("No match found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top