Saturday, September 22, 2012

Interesting Applications (Search and Highlight Text in a RichTextBox)

I promised you that today I'm gonna bring a interesting application. So here it is, in this application user will write something or paste something in the richTextBox control. Then he will provide a search keyword in the search textBox and will press the search button. The application will highlight the occurrence text in richTextBox and also show the number of occurrence in another textBox. So lets build the UI.
My UI looks like this:
Now lets deal with the business of coding

1. Double click the searchButton and write down the following code
            int total = 0;
            string searchKey = searchKeyTextBox.Text;
            string article = articleRichTextBox.Text;
            int indexPosition = article.ToLower().IndexOf(searchKey.ToLower());
            int searchKeyLength = searchKey.Length;

            while (indexPosition != -1)
            {

                articleRichTextBox.Select(indexPosition,searchKeyLength);
                articleRichTextBox.SelectionBackColor = Color.LightBlue;
                articleRichTextBox.SelectionColor = Color.White;
                indexPosition = article.IndexOf(searchKey, indexPosition + searchKeyLength);
                total = total + 1;
               
            }
            numberOfOccurrenceTextBox.Text = total.ToString();
            searchKeyTextBox.Text = string.Empty;

2. Now let me describe what is going on here
3. I took a variable name total to trace down the number of search keyword occurrence.
4. I took two string type variables and set the value of one to the text string of the searchKeyTextBox.Text and the second to the articleRichTextBox.Text.
5. I took a integer type variable indexPosition and  set the value to
=> article.ToLower().IndexOf(searchKey.ToLower());
6. It just took the value in article string set all the letter in the string to lowercase then it finds out the index (which is of course an integer)of the seacrhKey string (the first occurrence index of the searched word)
7. Here I use the ToLower() method cause if I make all the letter in the article and searchKey to lowercase. I can find the occurrence even if someone use Uppercase when they search.
8. To take the the length of the Searched key I took a variable called searchKeyLength.
9. Then I initiate a while loop.
10. This loop continue until the value of the indexPosition is -1.
11. Why til -1? Cause the IndexOf() method always returns a -1 if it doesn't find any starting index of the given text in the first brackets.(Means the word doesn't exist in the context.
12. Then the code inside the loop articleRichTextBox.Select(indexPosition,searchKeyLength); just select the searched text from its first index to its last index.
13. After That it colorize the Text. Change the color value and see changes.
14. Then it increment the indexPosition to the position after the first occurrence of the searched text.
15. Increment the total.
16. After the loop executes successfully I just paste the value of the total in the occurrenceTextBox.
17. The last line only vanishes the text from the searchKeyTextBox after the searchButton is pressed.
18. There is a problem in the code. What?
19. After debugging if you search a text for the first time it will show the highlighted text in the richTextBox.
20. But when you try with a different word. The first selection will also be there.
21. This problem can be solve by writing three lines at the beginning which will just select all the text set them to default color and then will find the searched text and highlight it.
            articleRichTextBox.SelectAll();
            articleRichTextBox.SelectionBackColor = Color.White;
            articleRichTextBox.SelectionColor = Color.Black;

Here I provide the full Application. You will find that I named all the objects,variable,functions according to the C# naming convention. Its a good practice for you if you want to be a good programmer.
Download Here 

No comments:

Post a Comment