Click here to Skip to main content
15,905,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my solution find and scroll to that found text but it doesn't selects all searched words
these select just a word

What I have tried:

C#
richTextBox1.Focus();
int start = richTextBox1.Text.IndexOf(textBox1.Text[0]);
int end = textBox1.Text.Length;
richTextBox1.Select(start, end);
Posted
Updated 23-Apr-19 3:53am

1 solution

String.IndexOf finds only the first occurrence in the string, so you need to repeat teh search using the overloaded methods.
public static class MyExtensions
    {
    public static List<int> AllIndexesOf(this string s, string lookFor)
        {
        List<int> indexes = new List<int>();
        int index = s.IndexOf(lookFor);
        int len = lookFor.Length;
        while (index >= 0)
            {
            indexes.Add(index);
            index = s.IndexOf(lookFor, index + len);
            }
        return indexes;
        }
    }
Will find them all:
List<int> x = "abcdabceab".AllIndexesOf("abc");
will return a List of two indexes.

But ... you can't select more than one text area - you will have to manually (and temporarily) highlight them yourself, or provide a "find next" function as some text editors do.
 
Share this answer
 
Comments
Member 14145167 23-Apr-19 11:13am    
How do I create the "find next" function
OriginalGriff 23-Apr-19 11:17am    
By saving the "last index" position and searching from there - in a similar way to that in my code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900