Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a rich text box there are 10 sentences written line by line and i have also one text box where i write a word to find in rich text box.
when i click on button then i want to highlight all specific word line by line in every click.
and if in any line there are two times that specific word then i want to highlight both word in a single click.

i am new in coding.
Please help me.


I am using this code it highlights all the specific word at a single click.
but now i want to search line by line in every click.

C#
int textEnd = txtshowfile.TextLength;
                    int index = 0;
                    int lastIndex = txtshowfile.Text.LastIndexOf(txtsearchgerman.Text);
                    while (index < lastIndex)
                    {
                        txtshowfile.Find(txtsearchgerman.Text, index, textEnd, RichTextBoxFinds.None);
                        txtshowfile.SelectionBackColor = Color.Yellow;
                        index = txtshowfile.Text.IndexOf(txtsearchgerman.Text, index) + 1;
                    }




Thanks in Advance friends.
Posted
Updated 26-Jan-12 23:20pm
v3

Here's a sketch of some code which, I hope, will get you started.

Put a RichTextBox, a Button, and a TextBox on a WinForm: name them: 'richTextBox1, 'button1, and 'textBox1:
C#
private string searchTerm;

private string theLine;

private int maxLines;
private int targetLine = 0;
private int matchAt;
private int searchTermLength;
private int lineLength;
private int lineOffset = 0;

private void button1_Click(object sender, EventArgs e)
{
    maxLines = richTextBox1.Lines.Length;

    searchTerm = textBox1.Text;

    searchTermLength = searchTerm.Length;

    theLine = richTextBox1.Lines[targetLine];

    lineLength = theLine.Length;

    if(theLine.Contains(searchTerm))
    {
         matchAt = theLine.IndexOf(searchTerm);

         richTextBox1.Select(lineOffset + matchAt, searchTermLength);

         richTextBox1.SelectionBackColor = Color.Yellow;
         
         // at this point you really have all the information you need to 
         // to keep scanning the current line for more search term matches !
         // personally, I'd use a while loop starting here ...
    }
 
    // note we don't do anything here if no match is found in the current line

    targetLine ++;

    // consider why we must add #1 here
    lineOffset += lineLength + 1;

    if (targetLine == maxLines)
    {
        targetLine = 0;
        lineOffset = 0;
    }
}
This code example will find, line by line, the first example of the search term in the RichTextBox, and highlight it yellow.

What it does not do is:

1. give any indication that it just checked a line which did not have the search term: if you want it to "skip over" lines that have no search term match, and automatically advance to the next line: that's left for you to do.

2. find multiple matches of the search term: that's left to you to figure out, but once you have "isolated" the current line, and have the information you can see calculated in the code: I believe that will not be difficult.

3. deal with the case where the RichTextBox content is changed, and you need to reset all the parameters for searching. And un-highlight any currently highlighted matches ?

4. deal with the case where the search term has changed, and you need to reset all the parameters for searching. And un-highlight any currently highlighted matches ?

5. any of the type of error-checking and validation a truly robust solution would do.

A really "robust" solution may need to keep track of all currently highlighted match terms, so it's easy to un-highlight them ? I might consider building a List<int> of each highlight match, where the integer value was the absolute value of the offset of the start of the match in the RichTextBox.Text: given the assumption that the search-term length is a constant, then all you need to do is execute a foreach loop through the generic List<int>, and select the high-lighted word, and reset the color.

Or, much more simply, just select all the Text in the RichTextBox and change its SelectionBackColor to some default value ?
 
Share this answer
 
v3
Comments
Amir Mahfoozi 29-Jan-12 0:22am    
+5
If you want to search text on selected line then you have to maintain total number of lines and in which row your mouse is, and if you want to search whole text then these links will help you
http://support.microsoft.com/kb/176643[^]
http://www.dotnetcurry.com/ShowArticle.aspx?ID=146[^]
 
Share this answer
 

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