The Find method searches the whole string of the RichTextBox control if the start parameter is equal to the end parameter

Symptoms

The Find method of the RichTextBox control searches for the text that is specified in the str parameter, and then returns the first character location of the search string in the control. When the Find method searches for a string that is not in the text of the control, the Find property returns -1. When you use an overloaded RichTextBox control Find method as RichTextBox1.Find (string, start, end, options), and you specify the same value for the start parameter and the end parameter, the Find method searches all the text starting from the zero position. Therefore, the Find method returns the position of the search string that starts at the zero position. Similar behavior occurs when you set the start position of the RichTextBox.Find method to RichTextBox.Text.Length.

Cause

When you set the start parameter as RichTextBox.Text.Length, or you set the start parameter and end parameter to the same value, the common language runtime internally calls the Find method (string, RichTextBox.Text.Length, RichTextBox.Text.Length, options). When you set the start and end parameters to the length of the text, the Find method searches all the text in the RichTextBox control. When you call an overloaded Find method (string, start, options), the common language runtime internally calls the Find method (string, start, -1, options). Similarly, when you call an overloaded Find method (charset, start), the common language runtime calls Find (charset, start, -1). Therefore, all the text is searched.

Workaround

To work around this problem, set the end parameter to a value that is one greater than the start parameter while you call the Find method. For example, do not use the following call to the Find method:
Find(String, RichTextBox.Text.Length, options)
Instead, use this Find method code:
Find(String, RichTextBox.Text.Length, RichTextBox.Text.Length+1, options)

Status

This behavior is by design.

More Information

Steps to Reproduce the Behavior

  1. Create a new Windows application project by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.

    By default, Form1 is created.
  2. Insert a RichTextBox control and a Button control on Form1.
  3. Double-click Button1, and then add the following code in the Button1_Click event:

    Visual Basic .NET or Visual Basic 2005
       Dim iFound As Integer
    RichTextBox1.Text = "Where there is a will there is way"
    iFound = RichTextBox1.Find("is", RichTextBox1.Text.Length, RichTextBox1.Text.Length, RichTextBoxFinds.None)
    MessageBox.Show(iFound.ToString())
    Visual C# .NET or Visual C# 2005
       int iFound;
    richTextBox1.Text = "Where there is a will there is way";
    iFound = richTextBox1.Find("is", richTextBox1.Text.Length, richTextBox1.Text.Length, RichTextBoxFinds.None);
    MessageBox.Show(iFound.ToString());
  4. On the Debug menu, click
    Start to run the application.
  5. Click Button1. Verify that the Find method searches all the text in the RichTextBox control.

References

For more information, see your .NET Framework SDK Documentation or visit the following MSDN Web site:

RichTextBox.Find Method
http://msdn2.microsoft.com/en-us/library/system.windows.forms.richtextbox.find(vs.71).aspx
Propriedades

ID do Artigo: 814317 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários