8 Replies - 4518 Views - Last Post: 02 August 2012 - 04:37 PM Rate Topic: -----

#1 Adidas 777  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 29-July 12

Search through whole Rich Text Box

Posted 29 July 2012 - 08:21 AM

Hi everyone
I really stuck not knowing how to search a whole rich text box... :(
I need to write something like

while ( you didn't got to the end of the richTextBox1 continue marking my wanted word)
{
...
} 



I already can display my .txt file to the rich text box but when I'm running a search I can mark only 1 word...
Can you please explain me how I can iterate over the whole content please ?
I'm adding my whole branch of code (no virus 4 sure) and also my code attached.

The whole project : https://dl.dropbox.c...richtextbox.rar
The cs code alone : https://dl.dropbox.c...6215/search.txt

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Search through whole Rich Text Box

#2 Adidas 777  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 29-July 12

Re: Search through whole Rich Text Box

Posted 29 July 2012 - 08:36 AM

What I want to get
Hi I trying to mark the entire rich text box wee
Also wee this line
I want to understand wee the way
Please advise wee


What i currently have
Hi I trying to mark the entire rich text box wee
Also wee this line
I want to understand wee the way
Please advise wee
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3456
  • View blog
  • Posts: 10,661
  • Joined: 05-May 12

Re: Search through whole Rich Text Box

Posted 29 July 2012 - 10:46 AM

You really should paste you code in here.

Anyway the easy way to change your code is switch to using String.IndexOf() that takes a start position: http://msdn.microsof...y/ms224423.aspx After you find a match, search for the next match at a position beyond the current match.

You are going about finding strings the slow inefficient way, though. The RichEdit control is an unmanaged window control. Each time you dod RichTextBox.Text, the system has to marshal the entire contents of the RichEdit from unmanaged code to managed code, create a new string, and return that to you.

The more efficient way to search the RichTextBox, is to use RichTextBox.Find(). Again, there are several flavors, but the one you'll probably need is the flavor that takes a start position so that you can find the other matches after the current match: http://msdn.microsof...y/9be0980x.aspx
Was This Post Helpful? 0
  • +
  • -

#4 Adidas 777  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 29-July 12

Re: Search through whole Rich Text Box

Posted 02 August 2012 - 10:35 AM

Quote

The more efficient way to search the RichTextBox, is to use RichTextBox.Find(). Again, there are several flavors, but the one you'll probably need is the flavor that takes a start position so that you can find the other matches after the current match:


Can you please give me an example because I didn't understood the point from the MSDN site ... :/

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3456
  • View blog
  • Posts: 10,661
  • Joined: 05-May 12

Re: Search through whole Rich Text Box

Posted 02 August 2012 - 01:07 PM

Look at the RexPad tutorial where he implements the basic Find operation: http://www.dreaminco...l-step-by-step/
Was This Post Helpful? 0
  • +
  • -

#6 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: Search through whole Rich Text Box

Posted 02 August 2012 - 04:12 PM

This should do what you are looking for:
FindIt(richTextBox1, "Value to search for", 0);

int FindIt(RichTextBox toSearch = null, String Srch = "", int Start = 0)
        {
            int retval = 0; // Instr returns a int
            String Source = ""; // variable used in Instr

            if (Srch.Trim().Length <= 0)
            {
                return 0;
            }

            if (Srch.Trim().Length > 0)
            {
                Source = toSearch.Text; // put the text to search into the variable

                if (Start == 0)
                {
                    Start = 1; // the initial call doesn't pass a value for Start, so it will equal 0
                    retval = toSearch.Find(Srch.Trim(), Start, toSearch.Text.Length, RichTextBoxFinds.None);

                    if (retval > 0) //there is at least one more occurrence of the string
                    {
                        // the RichTextBox doesn't support multiple active selections, so
                        // this section marks the occurrences of the search string by
                        // making them Bold and Red

                        toSearch.Selectionstart = retval;
                        toSearch.SelectionLength = Srch.Trim().Length;
                        toSearch.SelectionBackColor = Color.Yellow;

                        Start = retval + Srch.Trim().Length; // move the starting point past the first occurrence

                        // FindIt calls itself with new arguments
                        // this is what makes it Recursive
                        if (Start < toSearch.Text.Length)
                        {
                            FindIt(toSearch, Srch.Trim(), Start + 1);
                        }
                    }
                }
                else
                {
                    return 0; // nothing to highlight.. 
                }

            }
                return retval;
        }

    void resetSearch(RichTextBox toSearch) {
        toSearch.SelectAll();
        toSearch.SelectionBackColor = Color.White;
        toSearch.DeselectAll();
    }



You can also use the resetSearch() to reset the highlighting
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3456
  • View blog
  • Posts: 10,661
  • Joined: 05-May 12

Re: Search through whole Rich Text Box

Posted 02 August 2012 - 04:25 PM

That is a relatively good example of using RichTextBox.Find().

A couple of issues, though:
You were avoiding the marshalling cost by calling RichTextBox.Find(), but you still accidentally incurred it on line 15, and for no reason at all since Source is never used.

Also, I suspect that bit of code will not work correctly if the RichTextBox control contained:
William And Mary


and you searched of "William" and the start position is with the caret to the left of the 'W'.
Was This Post Helpful? 1
  • +
  • -

#8 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: Search through whole Rich Text Box

Posted 02 August 2012 - 04:30 PM

Ahh yes you are correct if there was not a space in the front of that first word it would not find "William", good catch!
Was This Post Helpful? 0
  • +
  • -

#9 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: Search through whole Rich Text Box

Posted 02 August 2012 - 04:37 PM

"Start" should be 0, and it looks like I make it 1 when it is 0, if we take that out it will resolve the issue of not being able to find the word if it resides at 0 index.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1